• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static variable issue

 
Greenhorn
Posts: 17
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does this: gives a compile time error

whereas :



compiles filne?

Using JDK 1.6_20
 
Greenhorn
Posts: 16
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The last one will compile, but it will cause an ExceptionInInitializerError at runtime caused by a NullPointerException, because you're trying to access an array, that hasn't been inizialized yet.
 
Trishul P Mukherjee
Greenhorn
Posts: 17
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks - but the question was not that. I'm not trying to run the classes. It's a compile time error I'm getting in the first case. Basically, what I wanted to know is - why the error in case of array a[] and not in case of a primitive variable v.
 
Ranch Hand
Posts: 148
Hibernate Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Trishul,

I tried your piece of code.You are correct ,I dont know why compiler complains in first condition.

But the following code compiles fine.


anybody know why?

Thanks
 
Trishul P Mukherjee
Greenhorn
Posts: 17
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah Vijay - thanks for adding on to this. Now this is even more confusing ! I don't have an answer yet
 
Rancher
Posts: 436
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can read about it here. With fields lexikographic order is relevant. When accessing via the full qualified name (TestStatic.v = 0;) this does not apply.

It is a security measure to prevent circular dependencies.
 
Ranch Hand
Posts: 103
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi trishul,

The reason for this is, as all are static members, they are loaded in the order in which they appear in the code,

so when you use the static block before the static array reference, the compiler complains as it cannot find the reference varribale.
On the contrary, if you reverse the order it works fine, as the reference varriable has already been loaded.

However when you are using the TestStatic.a[0] the order doesnt make any difference!!!

And your code will also throw an exception as has been already hinted!!!..
 
Trishul P Mukherjee
Greenhorn
Posts: 17
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jishnu, Thanks for your reply - agreed to your explanation for the static array reference.

But what happens to the variable 'v'? The same is not applicable for it? Shouldn't the compiler complain for the variable v in the second case, since it is being assigned a value in the static block even before it has been declared?
 
Greenhorn
Posts: 28
1
Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, According to my understanding,
while a compiler compiles a static initialization block (or any java code), when it comes across any reference variable, it tries to load the class of that type
and not actually instantiating or accessing the reference....,
but for primitives it is not the case.....(forward reference is okay for primitives.....)



Does this make ANY sense to Anybody..... Soryy I am not able to put it in better words.......
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rohini Sahuji wrote:but for primitives it is not the case.....(forward reference is okay for primitives.....)



No, I don't think so. Please read the JL specifications and here, maybe this will answer your questions.
 
Rohini Sahuji
Greenhorn
Posts: 28
1
Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Hauke!!
This really helped to clarify my understanding.


Thanks,
 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a bit puzzled by this Hauke.

According to JLS as you referenced:

8.3.2.3 Restrictions on the use of Fields during Initialization
The declaration of a member needs to appear before it is used only if the member is an instance (respectively static) field of a class or interface C and all of the following conditions hold:

The usage occurs in an instance (respectively static) variable initializer of C or in an instance (respectively static) initializer of C.
The usage is not on the left hand side of an assignment.
C is the innermost class or interface enclosing the usage.

A compile-time error occurs if any of the three requirements above are not met.



Now what puzzles me is that we are indeed using the reference on the left hand side of an assignment. Compare it with this



Basically setting the value of the member 'a' is fine, which is correct according to the rules, it is on the left hand side of an assignment so no problem.
The problem occurs when trying to set the value of a position inside the array, not setting the value of the array itself. If we consider the following instead:



This also compiles, but only if we reference 'a' by its longname, the same rule that applies when we try to read a member before it is initialized, it works only with the longname but produces an illegal forward reference error if we do it by its short name. (Note that this code will instead give a runtime exception, since the array reference points to null, its default initialization value.)

Personally I think the problem with...


..producing an illegal forward reference is somewhat of a special case of the "try to read before initialization", even though we're actually trying to set something to a value. My guess would be that in order to set a position in an array to a value, the array reference itself has to be read (in order to find the array object on the heap). This reading is what causes the illegal forward reference.

// Andreas
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andreas Svenkson wrote:
According to JLS as you referenced:

8.3.2.3 Restrictions on the use of Fields during Initialization
The declaration of a member needs to appear before it is used only if the member is an instance (respectively static) field of a class or interface C and all of the following conditions hold:

The usage occurs in an instance (respectively static) variable initializer of C or in an instance (respectively static) initializer of C.
The usage is not on the left hand side of an assignment.
C is the innermost class or interface enclosing the usage.

A compile-time error occurs if any of the three requirements above are not met.



Now what puzzles me is that we are indeed using the reference on the left hand side of an assignment. Compare it with this

 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andreas wrote: Now what puzzles me is that we are indeed using the reference on the left hand side of an assignment



Hello Andreas, I will appreciate it if you would be kind enough to specify the EXACT part in the codes the above statement. Thanks.
reply
    Bookmark Topic Watch Topic
  • New Topic