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.
public class Without_Main
{
static
{
System.out.println("Hello World!!");
System.exit(0);
}
//Does not work with JDK7
}
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.
Related Posts:
- Practice Information Technology MCQs
- Practice BCA MCQs
- Apply for Java Internship
- Check Java Books
- Practice Programming MCQs