---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 14 Edition"
java_version: 14
chapter: "Chapter 6. Names"
section: "6.6"
heading: "6.6. Access Control"
parent: "Chapter 6. Names"
source: https://docs.oracle.com/javase/specs/jls/se14/html/jls-6.html#jls-6.6
id: "6.6"
url: https://jvm.obfuscated.software/jls/se14/6.6.html
raw: https://jvm.obfuscated.software/jls/se14/6.6.md
---

# 6.6. Access Control

The Java programming language provides mechanisms for *access control*, to prevent the users of a package or class from depending on unnecessary details of the implementation of that package or class. If access is permitted, then the accessed entity is said to be *accessible*.

Note that accessibility is a static property that can be determined at compile time; it depends only on types and declaration modifiers.

Qualified names are a means of access to members of packages and reference types. When the name of such a member is classified from its context ([§6.5.1](6.5.1.md)) as a qualified type name (denoting a member of a package or reference type, [§6.5.5.2](6.5.5.2.md)) or a qualified expression name (denoting a member of a reference type, [§6.5.6.2](6.5.6.2.md)), access control is applied.

For example, a single-type-import declaration uses a qualified type name ([§7.5.1](7.5.1.md)), so the named type must be accessible from the compilation unit containing the `import` declaration. As another example, a class declaration may use a qualified type name for a superclass ([§8.1.5](8.1.5.md)), so again the named type must be accessible.

Some obvious expressions are "missing" from context classification in [§6.5.1](6.5.1.md): field access on a *Primary* ([§15.11.1](15.11.1.md)), method invocation on a *Primary* ([§15.12](15.12.md)), method reference via a *Primary* ([§15.13](15.13.md)), and the instantiated class in a qualified class instance creation ([§15.9](15.9.md)). Each of these expressions uses identifiers, rather than names, for the reason given in [§6.2](6.2.md). Consequently, access control to members (whether fields, methods, or types) is applied *explicitly* by field access expressions, method invocation expressions, method reference expressions, and qualified class instance creation expressions. (Note that access to a field may also be denoted by a qualified name occuring as a postfix expression.)

In addition, many statements and expressions allow the use of types rather than type names. For example, a class declaration may use a parameterized type ([§4.5](4.5.md)) to denote a superclass. Because a parameterized type is not a qualified type name, it is necessary for the class declaration to explicitly perform access control for the denoted superclass. Consequently, most of the statements and expressions that provide contexts in [§6.5.1](6.5.1.md) to classify a *TypeName* also perform their own access control checks.

Beyond access to members of a package or reference type, there is the matter of access to constructors of a reference type. Access control must be checked when a constructor is invoked explicitly or implicitly. Consequently, access control is checked by an explicit constructor invocation statement ([§8.8.7.1](8.8.7.1.md)) and by a class instance creation expression ([§15.9.3](15.9.3.md)). Such checks are necessary because [§6.5.1](6.5.1.md) has no mention of explicit constructor invocation statements (because they reference constructor names indirectly) and is unaware of the distinction between the class type denoted by an unqualified class instance creation expression and a constructor of that class type. Also, constructors do not have qualified names, so we cannot rely on access control being checked during classification of qualified type names.

Accessibility affects inheritance of class members ([§8.2](8.2.md)), including hiding and method overriding ([§8.4.8.1](8.4.8.1.md)).

## 6.6.1. Determining Accessibility

- If a top level class or interface type is declared `public` and is a member of a package that is exported by a module, then the type may be accessed by any code in the same module, and by any code in another module to which the package is exported, provided that the compilation unit in which the type is declared is visible to that other module ([§7.3](7.3.md)).

- If a top level class or interface type is declared `public` and is a member of a package that is not exported by a module, then the type may be accessed by any code in the same module.

- If a top level class or interface type is declared with package access, then it may be accessed only from within the package in which it is declared.

  A top level class or interface type declared without an access modifier implicitly has package access.

- A member (class, interface, field, or method) of a reference type, or a constructor of a class type, is accessible only if the type is accessible and the member or constructor is declared to permit access:

  - If the member or constructor is declared `public`, then access is permitted.

    All members of interfaces lacking access modifiers are implicitly `public`.

  - Otherwise, if the member or constructor is declared `protected`, then access is permitted only when one of the following is true:

    - Access to the member or constructor occurs from within the package containing the class in which the `protected` member or constructor is declared.
    - Access is correct as described in [§6.6.2](6.6.2.md).

  - Otherwise, if the member or constructor is declared with package access, then access is permitted only when the access occurs from within the package in which the type is declared.

    A class member or constructor declared without an access modifier implicitly has package access.

  - Otherwise, the member or constructor is declared `private`, and access is permitted if and only if it occurs within the body of the top level type ([§7.6](7.6.md)) that encloses the declaration of the member or constructor.

- An array type is accessible if and only if its element type is accessible.

**Example 6.6-1. Access Control**

Consider the two compilation units:

```
package points;
class PointVec { Point[] vec; }
```

and:

```
package points;
public class Point {
    protected int x, y;
    public void move(int dx, int dy) { x += dx; y += dy; }
    public int getX() { return x; }
    public int getY() { return y; }
}
```

which declare two class types in the package `points`:

- The class type `PointVec` is not `public` and not part of the `public` interface of the package `points`, but rather can be used only by other classes in the package.

- The class type `Point` is declared `public` and is available to other packages. It is part of the `public` interface of the package `points`.

- The methods `move`, `getX`, and `getY` of the class `Point` are declared `public` and so are available to any code that uses an object of type `Point`.

- The fields `x` and `y` are declared `protected` and are accessible outside the package `points` only in subclasses of class `Point`, and only when they are fields of objects that are being implemented by the code that is accessing them.

  See [§6.6.2](6.6.2.md) for an example of how the `protected` access modifier limits access.

**Example 6.6-2. Access to `public` Fields, Methods, and Constructors**

A `public` class member or constructor is accessible throughout the package where it is declared and from any other package, provided the package in which it is declared is observable ([§7.4.3](7.4.3.md)). For example, in the compilation unit:

```
package points;
public class Point {
    int x, y;
    public void move(int dx, int dy) {
        x += dx; y += dy;
        moves++;
    }
    public static int moves = 0;
}
```

the `public` class `Point` has as `public` members the `move` method and the `moves` field. These `public` members are accessible to any other package that has access to package `points`. The fields `x` and `y` are not `public` and therefore are accessible only from within the package `points`.

**Example 6.6-3. Access to `public` and Non-`public` Classes**

If a class lacks the `public` modifier, access to the class declaration is limited to the package in which it is declared (§6.6). In the example:

```
package points;
public class Point {
    public int x, y;
    public void move(int dx, int dy) { x += dx; y += dy; }
}
class PointList {
    Point next, prev;
}
```

two classes are declared in the compilation unit. The class `Point` is available outside the package `points`, while the class `PointList` is available for access only within the package. Thus a compilation unit in another package can access `points.Point`, either by using its fully qualified name:

```
package pointsUser;
class Test1 {
    public static void main(String[] args) {
        points.Point p = new points.Point();
        System.out.println(p.x + " " + p.y);
    }
}
```

or by using a single-type-import declaration ([§7.5.1](7.5.1.md)) that mentions the fully qualified name, so that the simple name may be used thereafter:

```
package pointsUser;
import points.Point;
class Test2 {
    public static void main(String[] args) {
        Point p = new Point();
        System.out.println(p.x + " " + p.y);
    }
}
```

However, this compilation unit cannot use or import `points.PointList`, which is not declared `public` and is therefore inaccessible outside package `points`.

**Example 6.6-4. Access to Package-Access Fields, Methods, and Constructors**

If none of the access modifiers `public`, `protected`, or `private` are specified, a class member or constructor has package access: it is accessible throughout the package that contains the declaration of the class in which the class member is declared, but the class member or constructor is not accessible in any other package.

If a `public` class has a method or constructor with package access, then this method or constructor is not accessible to or inherited by a subclass declared outside this package.

For example, if we have:

```
package points;
public class Point {
    public int x, y;
    void move(int dx, int dy) { x += dx; y += dy; }
    public void moveAlso(int dx, int dy) { move(dx, dy); }
}
```

then a subclass in another package may declare an unrelated `move` method, with the same signature ([§8.4.2](8.4.2.md)) and return type. Because the original `move` method is not accessible from package `morepoints`, `super` may not be used:

```
package morepoints;
public class PlusPoint extends points.Point {
    public void move(int dx, int dy) {
        super.move(dx, dy);  // compile-time error
        moveAlso(dx, dy);
    }
}
```

Because `move` of `Point` is not overridden by `move` in `PlusPoint`, the method `moveAlso` in `Point` never calls the method `move` in `PlusPoint`. Thus if you delete the `super.move` call from `PlusPoint` and execute the test program:

```
import points.Point;
import morepoints.PlusPoint;
class Test {
    public static void main(String[] args) {
        PlusPoint pp = new PlusPoint();
        pp.move(1, 1);
    }
}
```

it terminates normally. If `move` of `Point` were overridden by `move` in `PlusPoint`, then this program would recurse infinitely, until a `StackOverflowError` occurred.

**Example 6.6-5. Access to `private` Fields, Methods, and Constructors**

A `private` class member or constructor is accessible only within the body of the top level class ([§7.6](7.6.md)) that encloses the declaration of the member or constructor. It is not inherited by subclasses. In the example:

```
class Point {
    Point() { setMasterID(); }
    int x, y;
    private int ID;
    private static int masterID = 0;
    private void setMasterID() { ID = masterID++; }
}
```

the private members `ID`, `masterID`, and `setMasterID` may be used only within the body of class `Point`. They may not be accessed by qualified names, field access expressions, or method invocation expressions outside the body of the declaration of `Point`.

See [§8.8.10](8.8.10.md) for an example that uses a `private` constructor.

## 6.6.2. Details on `protected` Access

A `protected` member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.

### 6.6.2.1. Access to a `protected` Member

Let C be the class in which a `protected` member is declared. Access is permitted only within the body of a subclass S of C.

A subclass S is regarded as being responsible for the implementation of objects of class C. Depending on C's accessibility, S may be declared in the same package as C, or in different package of the same module as C, or in a package of a different module entirely.

In addition, access to an instance field or instance method is permitted based on the form of the qualified name, field access expression ([§15.11](15.11.md)), method invocation expression ([§15.12](15.12.md)), or method reference expression ([§15.13](15.13.md)):

- If the access is by (i) a qualified name of the form `ExpressionName.Id` or `TypeName.Id`, or (ii) a field access expression of the form `Primary.Id`, then access to the instance field `Id` is permitted if and only if the qualifying type is S or a subclass of S.

  The qualifying type is the type of the *ExpressionName* or *Primary*, or the type denoted by *TypeName*.

- If the access is by (i) a method invocation expression of the form `ExpressionName.Id(...)` or `TypeName.Id(...)` or `Primary.Id(...)`, or (ii) a method reference expression of the form `ExpressionName :: Id` or `Primary :: Id` or `ReferenceType :: Id`, then access to the instance method `Id` is permitted if and only if the qualifying type is S or a subclass of S.

  The qualifying type is the type of the *ExpressionName* or *Primary*, or the type denoted by *TypeName* or *ReferenceType*.

More information about access to `protected` members can be found in *Checking Access to Protected Members in the Java Virtual Machine* by Alessandro Coglio, in the *Journal of Object Technology*, October 2005.

### 6.6.2.2. Access to a `protected` Constructor

Let C be the class in which a `protected` constructor is declared and let S be the innermost class in whose declaration the use of the `protected` constructor occurs. Then:

- If the access is by a superclass constructor invocation `super(...)`, or a qualified superclass constructor invocation `E.super(...)`, where `E` is a *Primary* expression, then the access is permitted.
- If the access is by an anonymous class instance creation expression `new` C`(...){...}`, or a qualified anonymous class instance creation expression E`.new` C`(...){...}`, where E is a *Primary* expression, then the access is permitted.
- If the access is by a simple class instance creation expression `new` C`(...)`, or a qualified class instance creation expression E`.new` C`(...)`, where E is a *Primary* expression, or a method reference expression C `::` `new`, where C is a *ClassType*, then the access is not permitted. A `protected` constructor can be accessed by a class instance creation expression (that does not declare an anonymous class) or a method reference expression only from within the package in which it is defined.

**Example 6.6.2-1. Access to `protected` Fields, Methods, and Constructors**

Consider this example, where the `points` package declares:

```
package points;
public class Point {
    protected int x, y;
    void warp(threePoint.Point3d a) {
        if (a.z > 0)  // compile-time error: cannot access a.z
            a.delta(this);
    }
}
```

and the `threePoint` package declares:

```
package threePoint;
import points.Point;
public class Point3d extends Point {
    protected int z;
    public void delta(Point p) {
        p.x += this.x;  // compile-time error: cannot access p.x
        p.y += this.y;  // compile-time error: cannot access p.y
    }
    public void delta3d(Point3d q) {
        q.x += this.x;
        q.y += this.y;
        q.z += this.z;
    }
}
```

A compile-time error occurs in the method `delta` here: it cannot access the `protected` members `x` and `y` of its parameter `p`, because while `Point3d` (the class in which the references to fields `x` and `y` occur) is a subclass of `Point` (the class in which `x` and `y` are declared), it is not involved in the implementation of a `Point` (the type of the parameter `p`). The method `delta3d` can access the `protected` members of its parameter `q`, because the class `Point3d` is a subclass of `Point` and is involved in the implementation of a `Point3d`.

The method `delta` could try to cast ([§5.5](5.5.md), [§15.16](15.16.md)) its parameter to be a `Point3d`, but this cast would fail, causing an exception, if the class of `p` at run time were not `Point3d`.

A compile-time error also occurs in the method `warp`: it cannot access the `protected` member `z` of its parameter `a`, because while the class `Point` (the class in which the reference to field `z` occurs) is involved in the implementation of a `Point3d` (the type of the parameter `a`), it is not a subclass of `Point3d` (the class in which `z` is declared).
