Can we execute program without main method

Can we execute program without main method

Answer: Without main method you can not run java class as an application because at runtime java will search main method and don’t find it it will throw exception. Please see example class below:

package com.javahonk;

public class MainMethodTest {

    static {
	System.out.println("Static block");
    }    

}

 

You could compile above class but if you try run it from command prompt you will see below exception:
mainmethod2

Leave a Reply

Your email address will not be published. Required fields are marked *