• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on static initializers..

 
Rajasekar Elango
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rajagopal Manohar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: search this forum for "giveMeJ" (over the last 30 days)
[ December 16, 2004: Message edited by: Barry Gaunt ]
 
Shourya Kalinga
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rajasekar Elango
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34218
341
Eclipse IDE Java VI Editor
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic