When JVM finds a class, it first executes the static block and then searches for main method.
So, to avoid the JVM from searching for main method, we can execute the required code (iff possible) in static block and then exit .. before the main method is called.
Below is the code :
/**
* @Author : Pragya Rawal
*
*/
public class NoMain {
static {
System.out.println("I am in static block");
System.exit(0);
}
}
Please note that you would not be able to run this program using Eclispe IDE. You will have to run using command line
javac NoMain.java
java NoMain
you would see the output : "I am in static block"
So, to avoid the JVM from searching for main method, we can execute the required code (iff possible) in static block and then exit .. before the main method is called.
Below is the code :
/**
* @Author : Pragya Rawal
*
*/
public class NoMain {
static {
System.out.println("I am in static block");
System.exit(0);
}
}
Please note that you would not be able to run this program using Eclispe IDE. You will have to run using command line
javac NoMain.java
java NoMain
you would see the output : "I am in static block"
No comments:
Post a Comment