source file can't have more than one public class

后端 未结 6 1271
野的像风
野的像风 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:36

    The reason is, that this is the convention. Also the classloader expects a class in a specific file. You can write your own classloader to avoid this restriction, but there is no good reason to do this. Everyone looking on your code will get confused. ;)

    However, you can create "multiple" classes in one file by creating inner classes. I know, its not the same, but usually you should think about more important things than why there is only one class in one file.

提交回复
热议问题