source file can't have more than one public class

后端 未结 6 1263
野的像风
野的像风 2021-01-12 10:02

we can declare only one public file in a source file and file name must match the public class name

is there any reason to this restriction....

6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-12 10:31

    @saravanan. I have executed some simple programs which show that just a single default class(ie a class with no access specifier) having the main method works well in java when u save the file name with the default class name.

    To add on to the reason of naming the file with public class name, in a document I went through the details into this state that the JVM looks for the public class (since no restrictions and can be accessible from any where) and also looks for public static void main() in the public class .

    This public class acts as the initial class from where the JVM instance for the java application( program) is begun.So when u provide more than one public class in a program the compiler itself stops you by throwing an error.

    This is because later you can't confuse the JVM as to which class to be its initial class because only one public class with the public static void main(String args[]) is the initial class for JVM.

    HOPE I have helped you in understanding JAVA programming naming better.

提交回复
热议问题