Skip to main content

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.

A class in Java is a user-defined reference data type that acts as a blueprint for instantiating objects. It encapsulates state through fields (variables) and behavior through methods, establishing the structural and operational contract for all instances created from it. At the JVM level, a class is loaded by the ClassLoader subsystem. Its metadata (such as method bytecode and the constant pool) is stored in the Metaspace, while its instantiated objects and static variables reside on the Heap. Specifically, since Java 7, static variables are stored within the java.lang.Class object representing the class on the Heap.

Class Declaration Syntax

The signature of a Java class dictates its visibility, inheritance hierarchy, and polymorphic capabilities.
[access_modifier] [non_access_modifier] class ClassName [extends SuperClass] [implements Interface1, Interface2] [permits SubClass1, SubClass2] {
    // Class Body
}
  • access_modifier: Determines visibility (public or package-private/default).
  • non_access_modifier: Defines behavioral constraints. final prevents subclassing, and abstract prevents instantiation. As of Java 17 (JEP 409), sealed restricts which classes may extend it (requiring a permits clause), and non-sealed explicitly opens a previously sealed hierarchy for unrestricted extension. Historically, strictfp enforced IEEE 754 floating-point precision; however, as of Java 17 (JEP 306), all floating-point operations are strictly IEEE 754 by default, making the strictfp modifier obsolete.
  • class: The reserved keyword initiating the declaration.
  • ClassName: The identifier, which by convention adheres to PascalCase.
  • extends: Establishes single inheritance. A class can extend exactly one superclass. If omitted, the class implicitly extends java.lang.Object.
  • implements: Establishes interface realization. A class can implement multiple interfaces, separated by commas.
  • permits: Used in conjunction with the sealed modifier to explicitly declare the exhaustive list of allowed subclasses.

Class Members

The body of a class contains members that define its architecture and lifecycle.
  1. Fields: Variables scoped to the class.
    • Instance Variables: Allocated per object on the Heap. They maintain the unique state of an instance.
    • Static Variables: Allocated once per class on the Heap. Shared across all instances.
  2. Constructors: Special invocable blocks responsible for object initialization. They share the exact name of the class and have no return type. If no constructor is explicitly declared, the Java compiler injects a default, no-argument constructor.
  3. Methods: Functions that define behavior. Like fields, they can be instance-level (requiring an object context) or static (callable via the class reference).
  4. Initialization Blocks:
    • Static Blocks (static { ... }): Executed exactly once when the class is initialized by the JVM’s execution engine (via the <clinit> method). This initialization happens upon the class’s first active use (e.g., instantiation, or accessing a static member), following the ClassLoader’s loading phase.
    • Instance Blocks ({ ... }): Executed during the constructor’s execution. Specifically, they run immediately after the explicit or implicit call to the superclass constructor (super()) returns, and before the rest of the constructor’s body executes.
  5. Nested Types: Classes, interfaces, or records declared within the body of the enclosing class. They can be static or non-static (Inner Classes).

Structural Example

The following code demonstrates the mechanical components of a Java class without relying on a specific domain implementation.
import java.io.Serializable;

class AbstractEntity {
    public AbstractEntity() {
        // Superclass initialization
    }
}

public final class EntityNode extends AbstractEntity implements Serializable {

    // 1. Static Field (Class-level state)
    private static int nodeCount;

    // 2. Instance Fields (Object-level state)
    private final String nodeId;
    protected transient Object payload;

    // 3. Static Initialization Block
    static {
        nodeCount = 0;
    }

    // 4. Instance Initialization Block
    {
        this.payload = new Object();
    }

    // 5. Constructor
    public EntityNode(String nodeId) {
        super(); // Explicitly calls superclass constructor (this call is implicit if omitted)
        this.nodeId = nodeId;
        nodeCount++;
    }

    // 6. Instance Method
    public String getNodeId() {
        return this.nodeId;
    }

    // 7. Static Method
    public static int getNodeCount() {
        return nodeCount;
    }

    // 8. Static Nested Class
    public static class NodeComparator {
        public boolean compare(EntityNode n1, EntityNode n2) {
            return n1.getNodeId().equals(n2.getNodeId());
        }
    }
}

Instantiation Mechanics

To utilize a class as an object, it must be instantiated using the new keyword. This operator allocates memory on the Heap and initializes fields to their default values (e.g., null for references, 0 for integers). Next, it invokes the specified constructor. During the constructor’s execution, the superclass constructor is called first, followed by the execution of any instance initialization blocks, and finally, the remaining body of the invoked constructor is executed.
// Instantiating the class
EntityNode node = new EntityNode("ID-9942");

// Accessing an instance method via the object reference
String id = node.getNodeId();

// Accessing a static method via the class reference
int count = EntityNode.getNodeCount();
Master Java with Deep Grasping Methodology!Learn More