In what order java compiler checks the lines of the program. whether starting from the first line ? or starting from the main method?
You are mixing up execution and compilation which are two completely different, independent processes.
The compiler converts a source code file into one or more .class
files according to what it finds in the source files. This is independent from the execution of the resulting code. There is no other way as starting at the beginning of the file as every other artifact of the source file is unknown prior to compilation. Even the length of the lines and hence the position of all but the first line is unknown before reading the file. But note that compilation is a multiple-step process. At some point during the compilation, the sequential data is converted to a data structure, typical some kind of Abstract Syntax Tree, for which the original order of items in the source code is (mostly) irrelevant.
The execution of a Java application does not work on source code files but the compiled class files which are not organized in lines. In case you have compiled the classes with debug information enabled, there will be hints about which instructions map to which original source code line, but besides that there is no connection to lines. The .class
files have a binary format which has to be parsed by the JVM. This may start at the main class you have specified to the launcher but usually certain core classes like java.lang.Object
, java.lang.String
or java.lang.Thread
are preloaded before that.
Before the execution of the main
method can start, the class has to be resolved, which may include loading and resolving of other referenced classes, e.g. the super class of your main class. Then, the main class has to be initialized which includes execution of static initializers of the class and its super class(es). Then, the execution of the main
method may start. In case there are Java agents registered to the JVM, the startup process might be even more complicated.