Output of Java Programs | Set 48 (Static keyword)
Prerequisite : Static keyword in Java
Question 1. what is the output of this question?
class Test1 { public static void main(String[] args) { int x = 20; System.out.println(x); } static { int x = 10; System.out.print(x + " "); } } |
Option
A) 10 20
B) 20 10
C) 10 10
D) 20 20
Output: A
Explanation : Static block is executed before main method. If we declare a Static block in java class it is executed when class loads.
Question 2. what is the output of this question?
class Test1 { int x = 10; public static void main(String[] args) { System.out.println(x); } static { System.out.print(x + " "); } } |
Option
A) 10 10
B) Error
C) Exception
D) none
Output: B
Explanation : If we are trying to print the instance variable inside the static block or static method without creating class instance then it will give the error : non-static variable x cannot be referenced from a static context.
Question 3. what is the output of this question?
class Test1 { int x = 10; public static void main(String[] args) { Test1 t1 = new Test1(); System.out.println(t1.x); } static { int x = 20; System.out.print(x + " "); } } |
Option
A) 10 20
B) 20 10
C) 10 10
D) Error
Output: B
Explanation : We can print the instance variable inside the static method after creating the class reference.
Question 4. what is the output of this question?
class Test1 { int x = 10; public static void main(String[] args) { System.out.println(Test1.x); } static { int x = 20; System.out.print(x + " "); } } |
Option
A)10 10
B) 20 20
C) 20 10
D) Error
Output: D
Explanation : We can not access the instance variable with class name. otherwise it will give the error : non-static variable x cannot be referenced from a static context
Question 5. what is the output of this question?
class Test1 { static int x = 10; public static void main(String[] args) { Test1 t1 = new Test1(); Test1 t2 = new Test1(); t1.x = 20; System.out.print(t1.x + " "); System.out.println(t2.x); } } |
Option
A) 10 10
B) 20 20
C) 10 20
D) 20 10
Output: B
Explanation : static variable is class level variable. if we do update in any reference then automatically all pointing reference value are changed.
This article is contributed by Shivakant Jaiswal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Output of Java Programs | Set 44 (throws keyword)
- Output of Java Programs | Set 39 (throw keyword)
- Output of Java Programs | Set 45 (static and instance variables)
- Comparison of static keyword in C++ and Java
- Output of Java Programs | Set 50
- Output of Java Programs | Set 49
- Output of Java Programs | Set 32
- Output of Java Programs | Set 37 (If-else)
- Output of Java Programs | Set 30
- Output of Java Programs | Set 31
- Output of Java Programs | Set 12
- Output of Java programs | Set 29
- Output of Java programs | Set 13 (Collections)
- Output of Java Programs | Set 34 (Collections)
- Output of Java Programs | Set 47 (Arrays)

