Java Program without using the Main() Function

This is a Java Program to Print any Statement Without Using the Main Method.

We put the statement to be printed in a static block. The static block executes before main method hence we get the desired output. But in case of JDK7 the code would not execute as it looks for main method before any other thing.

Here is the source code of the Java Program to print any Statement Without Using the Main Method. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. public class Without_Main 
  2. {
  3.     static 
  4.     {
  5.          System.out.println("Hello World!!");
  6.          System.exit(0);
  7.     } 
  8.     //Does not work with JDK7
  9. }

Output:

$ javac Without_Main.java
$ java Without_Main 
 
Hello World!!

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.