Java entry point rules: what happens when main is not declared static? public class F0091 { public void main(String[] args) { System.out.println("Hello" + args[0]); } } // Command line: > java F0091 world
-
AHello
-
BHello Foo91
-
CHello world
-
DThe code does not run.
-
ECompilation fails due to println
Answer
Correct Answer: The code does not run.
Explanation
Introduction / Context: The Java Virtual Machine requires a specific entry point signature to launch an application: public static void main(String[] args). If the main method is not static, the JVM will not treat it as an entry point, even if the name “main” and parameter list match.
Given Data / Assumptions:
- The class declares public void main(String[] args) without static.
- Invocation is through the JVM launcher: java F0091 world.
- No other main method exists with the correct signature.
Concept / Approach: Because the JVM looks specifically for a static method, it cannot call an instance main automatically. The launcher reports a “Main method not found in class F0091” (or similar) and terminates without executing user code.
Step-by-Step Solution:
JVM loads class F0091.Searches for method: public static void main(String[]).Finds only public void main(String[]) → not acceptable.Launcher reports error; no “Hello world” is printed.Verification / Alternative check: Changing the signature to public static void main(String[] args) compiles and runs; output would be “Hello world”.
Why Other Options Are Wrong:
- Hello / Hello Foo91 / Hello world: All assume the program ran.
- Compilation fails due to println: System.out.println is valid; issue is the missing static modifier.
Common Pitfalls: Forgetting static, wrong parameter type (e.g., String args), or wrong access modifier prevents the application from starting.
Final Answer: The code does not run.