Output of Java Programs | Set 48 (Static keyword) - GeeksforGeeks

10 Oct, 2017
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 + " ");    }}
OptionA) 10 20B) 20 10C) 10 10D) 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 + " ");    }}
OptionA) 10 10B) ErrorC) ExceptionD) 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 + " ");    }}
OptionA) 10 20B) 20 10C) 10 10D) 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 + " ");    }}
OptionA)10 10B) 20 20C) 20 10D) 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);    }}
OptionA) 10 10B) 20 20C) 10 20D) 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.
Java-Output
Static Keyword
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C++ Programming Multiple Choice Questions
Output of C++ programs | Set 34 (File Handling)
Result of sizeof operator
Program to reverse columns in given 2D Array (Matrix)
Output of C++ Program | Set 1
Output of Java program | Set 26
Output of C++ programs | Set 22
unsigned specifier (%u) in C with Examples
Output of C Programs | Set 7
Output of C Programs | Set 6