Skip to content

Latest commit

 

History

History
28 lines (22 loc) · 676 Bytes

package_private_constructors.md

File metadata and controls

28 lines (22 loc) · 676 Bytes

Package-Private Constructors

A constructor without any other modifier is "package-private" in the same way as methods and fields.1

package dungeon;

public class Slime {
    final int size;

    // This constructor is public, 
    // code in other packages can use it.
    public Slime() {
        this.size = 5;
    }

    // This constructor is package-private,
    // code in other packages cannot use it.
    Slime(int size) {
        this.size = size;
    }
}

Footnotes

  1. Your spider-sense might be tingling wondering if private constructors are a thing. They are! I'll talk about them more in-depth later, but they can be surprisingly useful.