-3

When we say java Test, class loaders will load .class files into method area and static variables if any are initialized to corresponding values assigned and static blocks will be executed.

In java execution means code has to be interpreted by java interpreter. But how static block executes here without interpretation? It is still in a class loading phase, isn’t it?

But in most of the blogs and videos they say once after class loading completes, new thread will be created and it will look for main method and starts executing it. Interpreter comes to picture once main method starts execution in all blogs I saw.

class Test {

    static int x = 10;

    static {
         int y = 10;
        System.out.println(x);
    }

    public static void main(String[] args) {

    }
}
Holger
  • 285,553
  • 42
  • 434
  • 765
spa
  • 329
  • 1
  • 3
  • 15
  • 2
    please write properly. nobody will understand what you are trying to say – nilesh Jul 05 '18 at 07:05
  • 1
    Are you claiming that no code can or will be executed before `main()`? That's of course not true, no matter what you've read from the "bolgs". – Kayaman Jul 05 '18 at 07:07
  • Hi Kayaman, everywhere it is said whatever happens before main method execution starts is a class loading phase ,.but during class loading phase static block executes,how ? bcs for execution code has to be interpreted ryt . – spa Jul 05 '18 at 07:18
  • You're assuming that code can't be executed in the class loading phase. Your assumptions are wrong. – Kayaman Jul 05 '18 at 07:26
  • you mean soon after class loading completes and before main method starts execution static blocks will be interpreted and executed? – spa Jul 05 '18 at 07:33
  • 1
    After a class is loaded, its static blocks are executed. The main thread is in no way relevant here. The main method is **not** the first code that gets executed. – Kayaman Jul 05 '18 at 07:37

1 Answers1

3

The existence of an interpreter is an implementation detail that is irrelevant for describing the behavior of a program. In principle, it is possible to have a JVM without an interpreter at all, as it could have only a compiler that translates all byte code to native code before executing, and still implement the correct behavior. Current desktop and server JVMs have both, executing the code in a mixed mode.

So, it is also irrelevant at which point blogs and videos mention the existence of an interpreter as a way to execute the code, the execution of code always implies the existence of technical means to execute the code, like an interpreter or compiler.

The actual behavior has been specified in The Java Language Specification, §12.4.1:

§12.4.1. When Initialization Occurs

A class or interface type T will be initialized immediately before the first occurrence of any one of the following:

  • T is a class and an instance of T is created.
  • A static method declared by T is invoked.
  • A static field declared by T is assigned.
  • A static field declared by T is used and the field is not a constant variable (§4.12.4).
  • T is a top level class (§7.6) and an assert statement (§14.10) lexically nested within T (§8.1.3) is executed.¹

When a class is initialized, its superclasses are initialized (if they have not been previously initialized), as well as any superinterfaces (§8.1.5) that declare any default methods (§9.4.3) (if they have not been previously initialized). Initialization of an interface does not, of itself, cause initialization of any of its superinterfaces.

¹ this last bullet has been removed from newer specifications

Since the invocation of the main method is an invocation of “a static method declared by” your class, it implies the initialization of that class before the invocation. As you can derive from the last section, if the class containing the main method has uninitialized superclasses, they are initialized even before that class.

For the standard Java launcher, the order of events is

  • The main thread is created
  • The main thread loads the specified application class
  • The super classes of the application class are initialized, if not already initialized
  • The application class is initialized
  • The method static void main(String[]) is invoked on the application class

The terms “application class” and “main class” are interchangeable.

Note that this list is only meant to bring these events into the right order. There are far more events happening behind the scenes. Obviously, for asking the application class loader to load the application class, given by name, the classes String, Class, and ClassLoader must have been loaded and initialized, which also implies that their super class, Object has been initialized even before that. The existence of the main thread implies the loading and initialization of the class Thread. And all these classes use other classes behind the scenes.

You may run your application with the -verbose:class option to see which classes are already loaded before your application class.

Community
  • 1
  • 1
Holger
  • 285,553
  • 42
  • 434
  • 765
  • "In principle, it is possible to have a JVM without an interpreter at all, as it could have only a compiler that translates all byte code to native code" - IIRC, GCJ did exactly this - though it's been a dead project for many years. – Michael Berry Jul 05 '18 at 09:10
  • @MichaelBerry yeah, GCJ seems to be dead, but on the other hand, recent JDKs start experimenting with ahead-of-time compilation again; see [here](https://stackoverflow.com/q/45298045/2711488)… – Holger Jul 05 '18 at 09:13
  • Heh, that's interesting - that one somehow passed me by. I think GCJ was destined for failure not so much because of its goal of native execution, but because the team behind it was *tiny*, and they had a monumental, ever-evolving task. It'll certainly be interesting to see the direction the AOTC takes in the future. – Michael Berry Jul 05 '18 at 09:18
  • Thank you @Holger it helped lot. here in your answer you mentioned these lines "JVM without an interpreter at all, as it could have only a compiler that translates all byte code to native code before executing" compiler meand JIT compiler? right – spa Jul 05 '18 at 11:04
  • It could be a JIT compiler or an AOT compiler. Current JVMs have an interpreter and a JIT compiler. – Holger Jul 05 '18 at 11:27