File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Basic Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Basic Java" Watch "Basic Java" New topic

Basic Java

harish marathi

Joined: Jun 08, 2001
Posts: 2
I have a problem with static keyword
given a condition where
a)static block
b)static method
c)static variable
d)public static void main(String args[])
exists in a program.
What is the order of precedence of invocation ?
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
This is not really an IO and Streams question, so I've moved it to Java in General (beginner) for you.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

I am not sure what you mean by "invocation" in this context... I believe you mean "which exists first, and in what order do they get called"...

Basically all these things "exist" at the same time... when the JVM loads up the class, they are all created before you can access the class in any way.

The static block is executed whenever you first make any reference to the class it is in... create an object of the class? The static block is executed. Call a static method, or access a static variable? The static block is executed. Run a java class that has a static block? The static block is executed before main() begins.

However, in the static block, you can access static methods and variables, so you can technically think of them existing before the static block, but in reality, all these things "start existing" at the same time...

Technically, static methods ( Technically, the main() method falls into this category too... it is just a special case of a static method. ) are only invoked when you invoke them... the only exception to this is main()... the JVM knows that main() is the starting point of execution in a program...

Here is some code that may illustrate these concepts better than I have been at explaining them...

Try running StaticClass first, and then StaticClassTest and see the difference in output... hopefully this will help you see what is going on...


Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link:
subject: Basic Java
It's not a secret anymore!