aspose file tools
The moose likes Beginning Java and the fly likes static block called before executing static method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "static block called before executing static method" Watch "static block called before executing static method" New topic
Author

static block called before executing static method

Martin Lira
Ranch Hand

Joined: May 26, 2004
Posts: 97
Gurus,
I have a Class Test:


When I invoke the Test.testStaticMethod() (from some different class) the static block is executed first before the testStaticMethod. My question is why does that happen? And can I avoid that.

Thanks,
Martin
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I think that will always be the case.

TheClass.theMethod()

The JVM initializes TheClass before it calls theMethod. You wouldn't want it running methods on uninitialized classes! You have to be able to rely on static variables and such being ready before your method runs.

Hope that helps!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
You could put testStaticMethod() in a superclass of Test and give the static initializer block access to variables in the superclass containg the results of running testStaticMethod().

Here's what's happening. The very first (in execution order) reference to a class causes the ClassLoader to read the .class file and set up some basic class structures like the bytecode and the static variables in memory. All static initializers, both static initializer blocks and initializers of static variables contained in type declarations, are then executed in the order they appear in the source program. Only then is the first statement referring to the class executed.

If the class has superclasses that have not already been loaded, they must be loaded before the class in question. However, any subclasses that have not yet been referenced are not yet loaded.


Mike Gershman
SCJP 1.4, SCWCD in process
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Martin Lira:

When I invoke the Test.testStaticMethod() (from some different class) the static block is executed first before the testStaticMethod. My question is why does that happen? And can I avoid that.


The whole point of a static initializer is that it is executed before any method is called. If you don't want that code to execute, you need to put it elsewhere.


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
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I suspect that it doesn't run *every* time you call the static method. It *should* only run the first time you call it. Why don't you check this out?

Also, if this still isn't the behavior you want, you should describe what you are trying to do. I'm sure someone here will have some good suggestions for alternative ways to go about it.

Keep coding!

Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: static block called before executing static method
 
Similar Threads
Regarding Block...
Static method modifier question
about static and instance initializer blocks with inheritance
Reference to objects