This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on static initializers.." Watch "Question on static initializers.." New topic
Author

Question on static initializers..

Rajasekar Elango
Ranch Hand

Joined: Sep 13, 2004
Posts: 105
Hi,

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 ?

Thanks,

Raja


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
Greenhorn

Joined: Dec 14, 2004
Posts: 19
I was reading the excellent explanation about this at http://radio.javaranch.com/channel/corey/2004/05/13/1084483439000.html. 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
Hi,

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.

Thanks,
Raja
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on static initializers..
 
Similar Threads
Forward Referencing
Another qstn for you all... Just to enforce the point..
Initializers...
accessing static values
Why is it so?