Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

Chapter 3-Static Variables, Methods And Blocks

1)· What is Static?

Static is a keyword used to indi9cate that a field belongs to a class rather than
an object. This means that only one
instance of that static member is created and shared among all instance of class.

2) · What is a benefit of static variable?

Static variables can be defined once and can be shared among all instance of the
class.If all class instance must
have common property then we can use static variable
a) It save saves memory
b) static variable can be accessed without instatntiation of class .

3) · Can we declare local variable as static?

NO, we caqnnot declare local variable as static as local variable has local scope
i.e. they are created and destroyed as soon
as the method ends whereas the static variables are class variable and and have
class level; scope.

4) · Why main() method is declared as static?

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.

In any Java program, the main() method is the starting point from where compiler
starts program execution. So, the
compiler needs to call the main() method.

If the main() is allowed to be non-static, then while calling the main() method JVM
has to instantiate its class.
While instantiating it has to call the constructor of that class, There will be
ambiguity if the constructor of that
class takes an argument.

Static method of a class can be called by using the class name only without
creating an object of a class.
The main() method in Java must be declared public, static and void. If any of these
are missing, the Java program
will compile but a runtime error will be thrown.

4) · Can we Overload static methods ?

Yes static methods can be overloaded. we have to make sure that the method signture
is different.

5) · Can we Override static methods ?

No, we cannot override static methods because method overriding is based on dynamic
binding at runtime and the static
methods are bonded using static binding at compile time. So, we cannot override
static methods.
The calling of method depends upon the type of object that calls the static method.
It means:

If we call a static method by using the parent reference, the original static
method will be called from the parent class.
If we call a static method by using the child referemce, the static method of the
child class will be called.
This is called method hiding.

6) output--- s1.num1 15 s1.num2 28 s2.num1 22 s2.num2 28

· What is the output of this question?


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

7) output---- x 10
x 20

· 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 + " ");
· }
· }

8) · Can We declare main class as static?

NO, we can only declare nested inner class as static

You might also like