File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static Initializer question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static Initializer question" Watch "Static Initializer question" New topic

Static Initializer question

haijun wang

Joined: Aug 18, 2000
Posts: 21
Hi My friends:
I have one question from Sreenivasa Kumar Majji's Sun Certified Java programer practice exam exam 1, still don't understand. Please give me a help. Thanks.
The question is:
1: public class Q8
2: {
3: int i = 20;
4: static
5: {
6: int i = 10;
8: }
9: public static void main(String[] args)
10: {
11: Q8 a = new Q8();
12: System.out.println(a.i);
13: }
14: }
A) Compilation error, variable "i" declared twice.
B) Compilation error, static initializers for initialization purpose only.
C) Prints 10.
D) Prints 20.
the answer is D.
Another question is:
if a variable is defined and initialized in the class, and the same variable is defined and initialized in a method in the same class. What is the result if I print this variable in the local method or in the class scope?
Claire Radford

Joined: Nov 02, 2000
Posts: 5
I think the answer is -you have some static code within the class Q8, which is run when the class is loaded. However, the variable i inside this static block is local to this block and hence goes out of scope after the code block ends. It does not affect class variable i.
Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
You have two different variables here but they have the same identifiers. The scope is different for both. The static variable belongs to the whole class hence if you are refering to a.i, you are actually refering to the non static variable and hence 20 gets printed. Try putting in this stmt in the static block: System.out.println(i) and you'll see 10 printed out. This is because any static code block is executed by the JVM when the class is loaded, even before any objects are instantiated and before main() runs.
Rajiv Ranjan
Ranch Hand

Joined: Sep 28, 2000
Posts: 61
Check this site.It has a wonderful explanation of Static Initializer.
haijun wang

Joined: Aug 18, 2000
Posts: 21
Thank you every one who gives me the detailed explanation for this basic questions.
Have a good day.
Thank you from deep heart.
yagnesh ashara

Joined: Nov 08, 2000
Posts: 2
Clear one thing in mind that anything declared within the static block is not static. The thing is that it is initialized before the main() method. So, here the variable is not static and you can have more then one copy of this variable. So, here first i is assigned value of 10 and after that its changed to 20. and hence the answer.
The answer to the second question is whenever you print the value of any variable the last value will be printed. So, you have to go line-by-line. And detemine the last value. Don't be confused with scope and all that thing.
Aru Ven
Ranch Hand

Joined: Sep 28, 2000
Posts: 199
System.out.println("i : " + i);
statment never works if given in a static block or static method. The compiler cribs saying that
C:\Java\bin>javac Can't make a static reference to nonstatic variable i in class Q8. System.out.println("i : " + i);
The value of i which is an instance varible is 20.
remember the order of execution......
static variables/blocks, contructor, instance variables/blocks.
So here first the
static {
int i = 10;
gets executed. i = 10.
after the contructor the instance variables/ block will be assigned or executed. Then a new int i with value 20 is assigned.
I agree. Here's the link:
subject: Static Initializer question
It's not a secret anymore!