• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Initialiser has me a bit puzzled

 
Daniel Clinton
Ranch Hand
Posts: 46
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been writing some code to explore initialisation order

If the S.o.p. is uncommented a compilation error occurs:
Initialisation.java:5: error: illegal forward reference

So, I may assign a value to a static field before the definition but cannot use the field?
Is that right?
It seems inconsistent with a similar situation in initialisation of an object
where an object's constructor first calls super()
and if super() calls a subclass method polymorphically
and references a field which is not yet explicitly initialised,
the default value is used.
Why is the default value of y not returned here?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Clinton wrote:So, I may assign a value to a static field before the definition but cannot use the field?

Apparently. There are a couple weird things in the space of initializers. (Don't worry, these edge cases aren't on the exam!)

Daniel Clinton wrote:Why is the default value of y not returned here?

That would be confusing. You'd expect this to print 10, right?



If y is defined, the assignment should take effect. If y isn't defined, it shouldn't compile. The real question is why y=10; compiles in the first place.
 
sameera kumarasingha
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you use non-static initilizers same thng happens

 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jls 7: Forward References During Field Initialization in link you can find some example
Use of class variables whose declarations appear textually after the use is sometimes restricted, even though these class variables are in scope (§6.3). Specifically, it is a compile-time error if all of the following are true:
The declaration of a class variable in a class or interface C appears textually after a use of the class variable;
The use is a simple name in either a class variable initializer of C or a static initializer of C;
The use is not on the left hand side of an assignment;
C is the innermost class or interface enclosing the use.

Use of instance variables whose declarations appear textually after the use is sometimes restricted, even though these instance variables are in scope. Specifically, it is a compile-time error if all of the following are true:
The declaration of an instance variable in a class or interface C appears textually after a use of the instance variable;
The use is a simple name in either an instance variable initializer of C or an instance initializer of C;
The use is not on the left hand side of an assignment;
C is the innermost class or interface enclosing the use.



and one more for this you can use static variable to initialize instance variable, regardless of where declarations appear textually (and it is simple)

 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


and with this rules you can use static and instance variale in constructors and method (you can't use instance variable in static context)
(The use is a simple name in either a class variable initializer of C or a static initializer of C - not), regardless forward declaration. (local variable , method parametr not in subject this topic)


C is the innermost class or interface enclosing the use.
(because inner classes not used here, this rule is true for all examples)
 
Daniel Clinton
Ranch Hand
Posts: 46
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't get over how helpful the community are on this forum. Thank you all so much

Jeanne Boyarsky wrote:There are a couple weird things in the space of initializers. (Don't worry, these edge cases aren't on the exam!)

Thanks Jeanne, that is indeed good to hear!!

sameera kumarasingha wrote:if you use non-static initilizers same thing happens

Interesting, I hadn't tested that case, thank you Sameera.

Sergej Smoljanov wrote:jls 7: Forward References During Field Initialization in link you can find some example

Sergej, Your explanation was very helpful - took me a while to get it but I have it now
plus it is all there in JLS §8.3.3 as you linked
Thank you very much for taking the time to help
 
Paweł Baczyński
Bartender
Posts: 1876
35
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please, don't write long lines.
I broke them up for you this time.
 
Daniel Clinton
Ranch Hand
Posts: 46
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a bit puzzled
Was that referring to my last post Pawel?
Too many quotes?
 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this was for me. (for comments in code)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic