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

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.

主站蜘蛛池模板: 湄潭县| 肃宁县| 新丰县| 双柏县| 揭西县| 泰宁县| 南昌县| 铜梁县| 阿拉善盟| 五家渠市| 资源县| 丽江市| 兴隆县| 柳江县| 景东| 泰州市| 伊宁市| 博爱县| 中方县| 施秉县| 宝丰县| 米易县| 太仆寺旗| 庆云县| 澄城县| 襄汾县| 建湖县| 于都县| 莱西市| 苍梧县| 紫金县| 织金县| 宁明县| 北流市| 南雄市| 兰溪市| 翼城县| 库车县| 桐庐县| 三穗县| 七台河市|