What if main method is inside “non public class” of java file?

后端 未结 7 2183
半阙折子戏
半阙折子戏 2020-11-30 12:40

I have a java file containing more than one class, out of which one is public. If main method is inside a non-public class. I can\'t run that java file. Why is that? and the

7条回答
  •  没有蜡笔的小新
    2020-11-30 13:02

    You can certainly override main method and it does not violate any compiler rules and hence you will not have any compiler errors.

    You check that inspite of the fact that you have more than one class a file that is declared as public is the name of the file you are trying to execute.

    This is a convention that the file should be named after the same class which is public in that code.

    Hence when you try to execute that class it does not have a main method from which it starts execution.So if you want to execute the main method in the non public class the only way to this is call that main from a main method of the public class.

提交回复
热议问题