Discussion in 'Java' started by fattaneh, Aug 28, 2011.

  1. fattaneh

    fattaneh Guest

    hi everybody
    is every body knows which method executes before main() and static()
    fattaneh, Aug 28, 2011
    1. Advertisements

  2. Eric Sosman

    Eric Sosman Guest

    On 8/28/2011 11:03 AM, fattaneh wrote:
    > 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.

    Eric Sosman
    Eric Sosman, Aug 28, 2011
    1. Advertisements

  3. Jan Burse

    Jan Burse Guest

    fattaneh schrieb:
    > 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.

    Jan Burse, Aug 28, 2011
  4. Roedy Green

    Roedy Green Guest

    On Sun, 28 Aug 2011 08:03:56 -0700 (PDT), fattaneh
    <> wrote, quoted or indirectly quoted someone who
    said :

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

    Methods called in static initialisers are called first. see

    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
    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)
    Roedy Green, Aug 31, 2011
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jerry051
    Oliver Sturm
    Aug 2, 2005
  2. Mike Wilmot
    Mike Wilmot
    Dec 15, 2003
  3. CW
  4. Carlos Oliveira
    Carlos Oliveira
    Aug 19, 2004
  5. Kyung won Cheon
    Kyung won Cheon
    Nov 21, 2008

Share This Page