method

E

Eric Sosman

hi everybody
is every body knows which method executes before main() and static()

Your question is unclear, and suggests a good deal of confusion.
I'll try to clear up what I think may be confusing you -- but, of
course, I may have misunderstood your misunderstanding!

First, "which method executes before main()." Be aware that there
may be many main() methods in a single Java program: each class can
supply its own, and there are usually many classes. Also, since
`public static void main(String[] args)' is a `public' method, that
means any code in any class anywhere can call it, perhaps more than
once. So there is no single answer to "which method executes before
main()," because many methods may do so, many times.

But let's consider "the" main() method, the one belonging to the
class you told the Java run-time to load and execute. The run-time
performs its own initialization (exactly what goes on is unspecified,
and varies from one JVM to the next), then it loads the target class,
then it initializes the target class, and then it calls the main()
method of that class. The steps involved in getting a class ready to
run are described in the Java Language Specification and the Java
Virtual Machine Specification. Those documents contain a lot of detail,
but the brief summary is: The JVM finds the class' byte code (perhaps
by looking on the hard drive or fetching it from a network connection
or in some other way), verifies the byte code, resolves references to
other classes (which may involve loading and initializing them), and
runs the class' static initializers (if any).

Second, "which method executes before static()." Java has no
`static()' construct at all, but I imagine you're referring to the
static initializers of a class. If so, the answer is "no method,"
because the initializers execute as part of loading and initializing
the class, and this is done by the JVM and not by any Java method.

If this doesn't help, please post again with a clearer explanation
of what you're trying to find out.
 
J

Jan Burse

fattaneh said:
hi everybody
is every body knows which method executes before main() and static()

The static initializers are immediately called before the
main() method is invoked for the first time. This might
be long after the class has been loaded, and also by another
thread than the thread that loaded the class.

Accordingly to the JLS an eager calling of initializers
is not allowed. This is in contrast to the linking of a
class which can be done eager or lazy after the loading
of the class and before its initialization.

http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.4.1

Bye
 
R

Roedy Green

hi everybody
is every body knows which method executes before main() and static()

Methods called in static initialisers are called first. see
http://mindprod.com/jgloss/initialisation.html

If you asking what happens at the JVM level, a method called clinit
(sp) gets called first. It contains all the static init code.
--
Roedy Green Canadian Mind Products
http://mindprod.com
The modern conservative is engaged in one of man's oldest exercises in moral philosophy; that is,
the search for a superior moral justification for selfishness.
~ John Kenneth Galbraith (born: 1908-10-15 died: 2006-04-29 at age: 97)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top