what is the difference between `public class` and just `class`?

前端 未结 8 1322
说谎
说谎 2020-12-12 18:27

I have noticed that if don\'t write public before a class its works same as like a public class. I can\'t understand why so? It should show a error

相关标签:
8条回答
  • 2020-12-12 19:01

    It works the same only because you are working with just probably one file and in the same package.

    If you have more than one package then you have the problem. The class that doesn't have "public" before the name of class cannot be created in another package. You cannot use its constructor. You just cannot access it outside of the package that the class was created in.

    0 讨论(0)
  • 2020-12-12 19:02

    If you don't give an access modifier it's by default package private access. The class is not accessible outside of the package. Ideally the JLS should've included a keyword for package access to avoid confusion and unintended consequences.

    Something like,

    default class Student{}
    
    0 讨论(0)
提交回复
热议问题