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

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.

主站蜘蛛池模板: 五家渠市| 阜城县| 湖南省| 宁晋县| 时尚| 乌鲁木齐市| 调兵山市| 神池县| 长海县| 房产| 美姑县| 蕉岭县| 徐州市| 宣武区| 威信县| 北辰区| 察隅县| 偃师市| 涞源县| 内江市| 招远市| 乌恰县| 云浮市| 化隆| 肇东市| 都兰县| 上杭县| 西昌市| 绿春县| 大理市| 库伦旗| 商洛市| 金华市| 清新县| 合山市| 沙湾县| 大化| 赣榆县| 阜城县| 兴隆县| 古交市|