0% found this document useful (0 votes)
22 views

Chapter 3-Static Variables, Meth

The document discusses static keywords in Java and provides answers to questions about static variables, methods, and classes. Key points covered include that static belongs to the class rather than instances, static variables make programs more memory efficient, local variables cannot be static but methods can be overloaded or overridden, and the main method is static so it can be called without object creation.

Uploaded by

Darade Sagar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Chapter 3-Static Variables, Meth

The document discusses static keywords in Java and provides answers to questions about static variables, methods, and classes. Key points covered include that static belongs to the class rather than instances, static variables make programs more memory efficient, local variables cannot be static but methods can be overloaded or overridden, and the main method is static so it can be called without object creation.

Uploaded by

Darade Sagar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Que:1 What is Static?

--->
The static keyword belongs to the class than an instance of
the class.
We can apply static keyword with variables, methods, blocks
and nested classes.

Que:2 What is a benefit of static variable


--->
It makes your program memory efficient.
Understanding the problem without static variable.

Que:3 Can we declare local variable as static?


--->
Local variables cannot be static in java,they can be made
final.
Variables are made static so they can be initialized at the
class loading time.
variable static,and local variables are accessed within the
methods or functions.

Que:4 Why main() method is declared as static?


--->
Main() is declared as static as it is directly call by the JVM
without creating
an object of the class in which the it is declared.
java main() method is always static, so that compiler can call
it without the creation
of an object or before the creation of an object of the class.

Que:5 Can we Overload static methods ?


--->
Answer is Yes. We can overload static methods. But remember that
the method signature must be different.

Que:6 Can we Override static methods ?


--->
No, we cannot override static method in Java because a static
method is resolved at compile time by the compiler.

Que:7 Can we declare local variable as static ?


--->
static local variables are not allowed in Java. For example,
following Java program fails in compilation with error Static
local variables are not allowed

Que:8 What is the output of this question?

public class StaticDemo1


{
int num1 = 6;
static int num2 = 10;
public static void main(String args[])
{
StaticDemo1 s1 = new StaticDemo1();
StaticDemo1 s2 = new StaticDemo1();
s1.num1 = 15;
s1.num2 = 17;
s2.num1 = 22;
s2.num2 = 28;
System.out.println(s1.num1 + " " + s1.num2 + " " +
s2.num1 + " "+ s2.num2);

}
}

Output:
-------> 15 28 22 28

Que:9 What is the output of this question?

class StaticDemo2
{
public static void main(String[] args)
{
int x = 20;
System.out.println(x);
}
static
{
int x = 10;
System.out.print(x + " ");
}
}

Output:
-------> 10 20

Que: 10 Can We declare main class as static?


--->
Yes, you can declare a class static in Java,
provided the class is inside a top-level class. Such clauses
are also known as nested classes.

You might also like