Check The below Code :
import Shashwat.TestJava;
public class Main extends TestJava {
public static void main(String s[]) {
System.out.println("Inside Orignal \"Main\" Method");
Main m = new Main();
}
public void Main() {
System.out.println("Inside Duplicate Main");
}
}
Now the point is that when I execute the program, This line runs
System.out.println("Inside Orignal \"Main\" Method");
After which I create a Main class Object using
Main = new Main();
As far as I have seen this should call the constructor which has to be named 'Main' for the Class is named Main. Now This is what it executes to
Inside Orignal "Main" Method
But I have created a constructor and it should print text out. So Why is it not printing ? Are Constructors not allowed in a class with a main method ?
It's not printing, because you've created a void
-returning method with a name Main()
, but in order to be a constructor it should rather be:
public Main() {
System.out.println("Inside Duplicate Main");
}