Why does Java allow us to compile a class with a name different than the file name?

前端 未结 8 1722
梦毁少年i
梦毁少年i 2020-12-22 19:20

I have a file Test.java and the following code inside it.

public class Abcd
{
        //some code here

}

Now the class does n

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-22 20:00

    I look at it the other way round. The natural state of affairs would be for the programmer to pick both the class name and the file name independently. Probably in order to simplify finding public classes from outside a package during compilation, there is a special restriction that a public class be in a file with the corresponding name.

提交回复
热议问题