Clarification Regarding Java Program Entry Point and Static Block Execution
Chen Liang
chen.l.liang at oracle.com
Wed Apr 30 23:55:14 UTC 2025
Hello Karan,
I believe the existing documentation and specification is correct in this aspect.
In Java Language Specification, Section 12.1.2 [1], and Java Virtual Machine Specification, Section 5.2 [2], both state that the initial class is initialized before main is invoked.
JLS:
> After Test is loaded, it must be initialized before main can be invoked.
JVMS:
> The Java Virtual Machine then links the initial class or interface, initializes it, and invokes the public static method void main(String[])
In particular, JLS 12.1.3 describes the execution of initializers for the main class, including static initializers. JLS 12.1.4 then describes the execution of the main method, which happens after the execution of the class initializer.
It might be the "guideline" that you found somewhere was unofficial and misrepresented the Java specifications.
P.S. You are mailing to the OpenJDK organization instead of Oracle. This list jdk-dev is for development information relevant to all parts of the JDK project in the OpenJDK question. If you have any question, please reply to me privately without cc'ing to this list.
Regards,
Chen
[1] https://docs.oracle.com/javase/specs/jls/se24/html/jls-12.html#jls-12.1.2
[2] https://docs.oracle.com/javase/specs/jvms/se24/html/jvms-5.html#jvms-5.2
________________________________
From: jdk-dev <jdk-dev-retn at openjdk.org> on behalf of Karan Sharma <karan03945 at gmail.com>
Sent: Wednesday, April 30, 2025 6:05 PM
To: jdk-dev at openjdk.org <jdk-dev at openjdk.org>
Subject: Clarification Regarding Java Program Entry Point and Static Block Execution
Dear Oracle Java Documentation Team,
I hope this message finds you well.
I have been studying Java and came across the commonly stated guideline that the main(String[] args) method serves as the entry point for all Java applications. However, during experimentation and further reading, I observed that static blocks in a Java class are executed before the main() method, and in some cases, it is even possible to run logic using only static blocks without defining a main() method (such as in earlier versions of Java or with specific class loaders).
This leads me to a conceptual query:
If static blocks are executed prior to main(), and code within them can run independently, is it still fully accurate to say that the main method is the only entry point of a Java program?
I understand that main() serves as the starting point for execution in standard standalone Java applications. However, since there are cases (e.g., static initialization, frameworks, reflection) where control can begin before or without main(), I believe it may be valuable to clarify this nuance in the official documentation — especially for learners trying to deeply understand Java's execution model.
I would greatly appreciate any insights or thoughts from your side on this topic.
Thank you for maintaining such comprehensive resources for Java developers worldwide.
Warm regards,
Karan Kumar
karan03945 at gmail.com<mailto:karan03945 at gmail.com>
B.Tech Student | Java Enthusiast
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/jdk-dev/attachments/20250430/86684439/attachment-0001.htm>
More information about the jdk-dev
mailing list