Skip to content

"Classes and Objects" tutorial (https://dev.java/learn/classes-objects/ ) discussion of access modifiers should probably be updated for changes with Java Platform Module System released in Java 9 (in 2017) #211

@willy-b

Description

@willy-b

Hello dev.java team!

Thanks again for all these wonderful articles.

It seems though there are excellent separate articles within dev.java on the impacts of the Java Platform Module System (JPMS; since Java 9 in 2017, https://openjdk.org/projects/jigsaw/spec/ ) when used for a project on access,
the "Classes and Objects" tutorial https://dev.java/learn/classes-objects/ has not yet been updated for the impact of modules on access. Also, that reflection can bypass access controls under some circumstances might be mentioned in passing (could link to e.g. https://dev.java/learn/reflection/fields/#accessible (archived as is at: https://web.archive.org/web/20260317205612/https://dev.java/learn/reflection/fields/#accessible )).


Regarding https://dev.java/learn/classes-objects/creating-classes/ (archived as is at https://web.archive.org/web/20260317200949/https://dev.java/learn/classes-objects/creating-classes/ ):

  • Re: "public modifier—the field is accessible from all classes." it seems this should be updated to account for the Java Platform Module System which, when used, only reveals to code outside the module the public modified items in packages exported by the module (or opened, if considering reflective access) (exports or opens can also be qualified to restrict such access only to other particular target modules).

[dev.java already has other articles on the impact of JPMS on access at https://dev.java/learn/modules/intro/ (archived as is at: https://web.archive.org/web/20260317203840/https://dev.java/learn/modules/intro/ ),
and https://dev.java/learn/modules/qualified-exports-opens/ (archived as is at: https://web.archive.org/web/20260317204235/https://dev.java/learn/modules/qualified-exports-opens/ ), and https://dev.java/learn/modules/opening-for-reflection/ (archived as is at: https://web.archive.org/web/20260317204550/https://dev.java/learn/modules/opening-for-reflection/ ).

See also:
https://docs.oracle.com/javase/specs/jls/se25/jls25.pdf

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).

Where §7.4.3 is 7.4.3 Package Observability and Visibility,
which states:

A package is visible to a module M if and only if an ordinary compilation unit
containing a declaration of the package is visible to M
[...]
A package is uniquely visible to a module M if and only if one of the following holds:
• An ordinary compilation unit associated with M contains a declaration of the
package, and M does not read any other module that exports the package to M.
• No ordinary compilation unit associated with M contains a declaration of the
package, and M reads exactly one other module that exports the package to M.

and re: classes the JLS 25 in 6.6.1 says

If a top level class or interface (§7.6) is declared public and is a member of a
package that is exported by a module, then the class or interface 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 class or
interface is declared is visible to that other module (§7.3).
• If a top level class or interface is declared public and is a member of a package
that is not exported by a module, then the class or interface may be accessed by
any code in the same module [me: including the unnamed module].

]


In https://dev.java/learn/classes-objects/more-on-classes/#controlling-access (archived as is at: https://web.archive.org/web/20260317203241/https://dev.java/learn/classes-objects/more-on-classes/#controlling-access )

Re: the table shown for "Controlling Access to Members of a Class", quoted below:

The following table shows the access to members permitted by each modifier.
Modifier Class Package Subclass World
public Y Y Y Y [should be updated for JPMS post Java 9, for projects using JPMS]
protected Y Y Y N
no modifier Y Y N N
private Y N N N

This "Controlling Access to Members of a Class" table should also probably be updated for the release of the Java Platform Module System (JPMS) in Java 9, which, when a project uses JPMS, restricts access to even public types and members by code outside the module based on which packages are exported by the module (or opened, concerning reflective access) (and exports or opens can be qualified to restrict such access only to other particular target modules).

[again, for ease of reference, dev.java already has other articles on the impact of JPMS on access at https://dev.java/learn/modules/intro/ (archived as is at: https://web.archive.org/web/20260317203840/https://dev.java/learn/modules/intro/ ), and https://dev.java/learn/modules/qualified-exports-opens/ (archived as is at: https://web.archive.org/web/20260317204235/https://dev.java/learn/modules/qualified-exports-opens/ ), and https://dev.java/learn/modules/opening-for-reflection/ (archived as is at: https://web.archive.org/web/20260317204550/https://dev.java/learn/modules/opening-for-reflection/ ).

See also:
https://docs.oracle.com/javase/specs/jls/se25/jls25.pdf

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).

]

It may also be useful to mention on this page as well that the package, protected, and private access checks can sometimes be bypassed via reflection under certain conditions (see https://dev.java/learn/reflection/fields/#accessible (archived as is at: https://web.archive.org/web/20260317205612/https://dev.java/learn/reflection/fields/#accessible ) (relevant: section was added in discussion at #191 (comment) )).


Thanks so much! Feel free to leave open even if you address the above as I would also be happy to confirm any changes before closing if desired.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions