Reputation: 117
My instructor used to tell me that Java programs start from the main function. However, is this true?
I mean, if there are any global variables declared, they get allocated memory before the main() begins, right?
Upvotes: 1
Views: 75
Reputation: 200236
Technically it is true that a Java program may be executed in entirety before the main method executes. Any program of the form
class X {
public static void main(String[] args) {
... any code as long as it doesn't refer to args ...
}
}
can be rewritten to
class X {
static {
... the same code ...
}
public static void main(String[] args) {
}
}
and have exactly the same behavior. Note that the main
method is still required, but it will execute after all other code of the program.
Upvotes: 3
Reputation: 144
You can find more details about this in the documentation here: http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html
Short version:
Upvotes: 2