CKTIS / Java
The keyword private
The keyword private: The private modifier restricts a member to the body of its declaring top-level class and that class’s nestmates.
Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime
Syntax
privateThe private modifier restricts a member to the body of its declaring top-level class and that class’s nestmates.
Hide state behind private and expose operations that preserve invariants instead of public mutable fields.
private is not a security boundary against trusted reflection, and excessive hiding can make a poor design harder to test.
A private constructor blocks ordinary external new but enables factories; private methods are not polymorphically overridden.
The factory reveals secret=7 and ctor-private=true confirms the private constructor.
Examples
public class Main { static final class Vault { private final int secret; private Vault(int n){secret=n;} static Vault of(int n){return new Vault(n);} private int reveal(){return secret;} } public static void main(String[] a){var v=Vault.of(7);System.out.println("secret="+v.reveal());System.out.println("ctor-private="+java.lang.reflect.Modifier.isPrivate(Vault.class.getDeclaredConstructors()[0].getModifiers()));} }Result
secret=7 ctor-private=true
The factory reveals secret=7 and ctor-private=true confirms the private constructor.