Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static initializer not executed

 
J. Nuno
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, can someone explain me why Son's static initializer doesn't run?



Thanks!
 
Paul Clapham
Sheriff
Posts: 21581
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at it the other way around. It didn't run, therefore the Son class was not loaded. So now your question becomes, why was Son not loaded? That would be because it was not needed. And why was it not needed?

... does that help?
 
J. Nuno
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul, thanks for the quick reply. You're right, Son's not needed because this was a simple example, my real situation is:



I was expecting i value to be 100 since I'm calling it as Son.method() but I guess I was wrong.
 
Darius Cooper
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The call to the static Son.method() is actually a call to Parent.method(), since Son does not override it. So, Son is not loaded. If you add an empty static method to Son, and call it from Base, just before the call to Son.method(), you'll see the value you want.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your case, Parent.method() and Son.method() will produce exactly the same byte code. (The eclipse compiler can be configured to flag the latter with a warning because of this.)
 
J. Nuno
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ilja,

I set that Eclipse flag you mentioned on and this is the warning:
"The static method method() from the type Parent should be accessed directly"

I understood your explanation, still, I think this is a strange behaviour...

Thank you!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic