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 Question on static initializers.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on static initializers.." Watch "Question on static initializers.." New topic

Question on static initializers..

Rajasekar Elango
Ranch Hand

Joined: Sep 13, 2004
Posts: 105

I am having problems in understanding the following code illustrated in JLS

and Ouput is 0.

Why there is no compilation error as variable j is used before declaration..?

Even if compiler can understand forward reference of j, why the output is not 1 as j is initialized to 1 during declaraion ?



SCJP 1.4
Rajagopal Manohar
Ranch Hand

Joined: Nov 26, 2004
Posts: 183
I guess the reason could be that the compilation takes place in 2 passes in the first pass the variables are identified(forward references).
after that assignment of values takes place as and when it happens
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Hint: search this forum for "giveMeJ" (over the last 30 days)
[ December 16, 2004: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Shourya Kalinga

Joined: Dec 14, 2004
Posts: 19
I was reading the excellent explanation about this at by Corey

In the following program, how does compiler know that 'i' is of integer type [Dont know exactly the order in which compiler starts checking]. However, at runtime how does JVM know about the type, if the initializers and members execute from top to bottom.

[ December 16, 2004: Message edited by: Shourya Kalinga ]
[ December 16, 2004: Message edited by: Shourya Kalinga ]

SCJP 1.4<br />SCWCD 1.4
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
A compiler has read your entire program and created a tree structure and symbol table before it starts to validate the types of the variables used in expressions.

The jvm knows the types of all the variables in a class from the .class object, which is in memory before any instance of the class begins to be created. Early in the object creation process, there is a point where memory has been allocated for all the member variables and they have been initialized to their default values (0, false, or null). After that, the initializers and initializer blocks are executed in the order they appear in your program. After that, the constructors execute.
[ December 16, 2004: Message edited by: Mike Gershman ]

Mike Gershman
SCJP 1.4, SCWCD in process
Rajasekar Elango
Ranch Hand

Joined: Sep 13, 2004
Posts: 105

Thanks everyone..

Looks like this is an interesting question and I could see lot of interesting discussions on this..

Also, making j as final produces output 1.

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33102

The article is now at

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link:
subject: Question on static initializers..
It's not a secret anymore!