File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Static initializer not executed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Static initializer not executed" Watch "Static initializer not executed" New topic
Author

Static initializer not executed

J. Nuno
Greenhorn

Joined: Jul 11, 2008
Posts: 13
Hi, can someone explain me why Son's static initializer doesn't run?



Thanks!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jul 11, 2008
Posts: 13
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

Joined: Jul 10, 2008
Posts: 8
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

Joined: Jul 11, 2001
Posts: 14112
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.)


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
J. Nuno
Greenhorn

Joined: Jul 11, 2008
Posts: 13
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static initializer not executed
 
Similar Threads
overriden method
can anyone explain me the output..??
private vs. public
question about polymorphism
clarification on why the modifier private changes output..