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

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.

主站蜘蛛池模板: 济南市| 永州市| 杭州市| 大同县| 贵定县| 民乐县| 绿春县| 普洱| 农安县| 安福县| 肥东县| 贞丰县| 赫章县| 彰化市| 铜川市| 墨竹工卡县| 永年县| 拜泉县| 常山县| 肃北| 阿坝| 平陆县| 射洪县| 长治县| 丰顺县| 黎城县| 梁河县| 遂川县| 专栏| 贵南县| 恩施市| 池州市| 彭阳县| 萝北县| 恩平市| 肥西县| 满洲里市| 确山县| 武强县| 金门县| 潮安县|