What is output of following code package r4r.co.in.core.java; public class HelloWorld { int i = 0; public static void main(String args[]) { System.out.println(i); new HelloWorld(); } static { int i; i = 10; System.out.println(i); } }

What is output of following code package r4r.co.in.core.java; public class HelloWorld { int i = 0; public static void main(String args[]) { System.out.println(i); new HelloWorld(); } static { int i; i = 10; System.out.println(i); } }

The above program give an error in compilation, like a non-static field can be referenced from a static context. The problem lies in the variable i in print statement inside the main()  method. The variable i can only be accessed through the object reference.

By:Jalees
Date:2016-01-09 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related Java Subjective Links

Java Subjective interview questions and answers for experienced and fresher