> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Java Package-Private Constructor

A package-private constructor in Java is a constructor declared without any explicit access modifier (such as `public`, `private`, or `protected`). It restricts the instantiation of the class strictly to other classes residing within the exact same package, enforcing package-level access control at the point of object creation.

## Syntax

To define a package-private constructor, omit the access modifier entirely from the constructor declaration:

```java theme={"dark"}
package com.system.core;

public class Configuration {
    
    // Package-private constructor
    Configuration() {
        // Initialization logic
    }
}
```

## Access Control Rules

The absence of an access modifier applies Java's "default" (package-private) visibility rules specifically to the constructor, independent of the class's visibility. Even if the class itself is declared `public`, a package-private constructor imposes the following restrictions:

1. **Same Package Access:** Any class located in the same package can invoke the constructor using the `new` keyword.
2. **Cross-Package Restriction:** Classes located in a different package cannot invoke the constructor. Attempting to do so results in a standard `javac` compile-time error: `ClassName(...) is not public in ClassName; cannot be accessed from outside package`.
3. **Subclassing Constraints:** If a superclass only defines package-private constructors, it cannot be subclassed by a class in a different package. Subclass constructors must invoke a superclass constructor (implicitly or explicitly via `super()`). If the superclass constructor is package-private, it is invisible to the cross-package subclass, causing a compilation failure.

## Code Visualization

The following example demonstrates the compilation boundaries enforced by a package-private constructor across distinct files:

**`Node.java`**

```java theme={"dark"}
package com.system.core;

public class Node {
    private String id;

    // Package-private constructor
    Node(String id) {
        this.id = id;
    }
}
```

**`NodeManager.java`**

```java theme={"dark"}
package com.system.core;

public class NodeManager {
    public Node createNode() {
        // VALID: NodeManager is in the same package as Node
        return new Node("node-01"); 
    }
}
```

**`Application.java`**

```java theme={"dark"}
package com.system.client;

import com.system.core.Node;

public class Application {
    public void initialize() {
        // INVALID: Compilation error. 
        // Application is in a different package, so the constructor is not accessible.
        Node myNode = new Node("node-02"); 
    }
}
```

## Implicit Constructor Behavior

If a class declares no constructors, the Java compiler automatically generates a no-argument default constructor. For standard classes, the access modifier of this compiler-generated constructor strictly matches the access modifier of the class itself. However, if the type is an `enum`, the implicitly generated default constructor is always `private`, regardless of the enum's access modifier (per JLS 8.9.2).

Therefore, if a standard class is `public`, its implicit default constructor is `public`. To enforce package-private instantiation on a `public` class, the package-private constructor must be explicitly declared to override the compiler's default behavior.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Java Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
