官术网_书友最值得收藏!

Modifiers

Methods, constructors, fields, interfaces, and classes can have access modifiers. The general rule is that in case there is no modifier, the scope of the method, constructor, and so on, is the package. Any code in the same package can access it.

When the private modifier is used, the scope is restricted to the so-called compilation unit. This means the class that is in one file. What is inside one file can see and use anything declared to be private. This way, inner and nested classes can have access to each other's private variables, which may not really be a good programming style, but Java permits that.

The opposite of private is public. It extends the visibility to the whole Java program, or at least to the whole module, if the project is a Java 9 module.

There is a middle way: protected. Anything with this modifier is accessible inside the package and also in classes that extend the class (regardless of package) that the protected method, field, and so on, is in.

主站蜘蛛池模板: 凌源市| 阿尔山市| 罗甸县| 嘉善县| 垣曲县| 合阳县| 田林县| 长乐市| 克东县| 旅游| 加查县| 自治县| 新安县| 辽源市| 略阳县| 怀化市| 双辽市| 米易县| 古浪县| 临邑县| 滦平县| 清新县| 通海县| 柳林县| 莆田市| 濮阳市| 九龙城区| 阳朔县| 凌云县| 大港区| 高淳县| 大余县| 新乡县| 孟村| 鹤岗市| 齐齐哈尔市| 方城县| 万盛区| 新田县| 淮南市| 锦州市|