- Create a Class with any class name.
- Write a Static block and write the statements to execute.
- At the end of the static block give System.exit(0), to avoid the exception in thread main() no main() method found.
- Now save the file with the [class_name].java.
- Finally compile and run.
The Demonstration of above steps is show bellow
- class nomain //step1
- {
- static//step 2
- {
- System.out.println("This is in Static method");//step3
- System.exit(0);//step 4
- }
- /* public static void main(String []args)
- {
- System.out.println("This won't execute");
- }*/
- }
running and compiling
javac nomain.java
java nomain.java
output :
This is in Static method