SyntaxStudy
Sign Up
Java Static Members, Inner Classes, and Enums
Java Beginner 1 min read

Static Members, Inner Classes, and Enums

Static members belong to the class itself. Static fields are shared across all instances; static methods can be called on the class name without creating an object. Static initializer blocks run once when the class is loaded. They are useful for initialising complex static fields that cannot be set in a single expression. Java supports several kinds of nested classes. A static nested class is declared inside another class with the static modifier; it does not hold a reference to its outer instance. A non-static inner class does hold such a reference and can access the outer class fields directly. Local classes are defined inside a method. Anonymous classes are local classes without a name, often used to implement interfaces or extend classes inline. Enums (enumerations) are a special kind of class where the instances are a fixed set of named constants. Enums can have fields, constructors, and methods. They are type-safe alternatives to integer constants and integrate with switch statements. The ordinal() method returns the zero-based position, name() returns the constant name, and values() returns an array of all constants.
Example
public class StaticAndEnum {

    // Enum with fields and methods
    enum Planet {
        MERCURY(3.303e+23, 2.4397e6),
        VENUS  (4.869e+24, 6.0518e6),
        EARTH  (5.976e+24, 6.37814e6),
        MARS   (6.421e+23, 3.3972e6);

        private final double mass;   // in kilograms
        private final double radius; // in metres

        Planet(double mass, double radius) {
            this.mass   = mass;
            this.radius = radius;
        }

        static final double G = 6.67300E-11;

        double surfaceGravity() {
            return G * mass / (radius * radius);
        }

        double surfaceWeight(double otherMass) {
            return otherMass * surfaceGravity();
        }
    }

    // Static nested class (no outer instance needed)
    static class Config {
        static final int MAX_RETRIES = 3;
        static final String DEFAULT_HOST = "localhost";
        // ...
    }

    // Static initializer block
    static final java.util.Map<String, Integer> CODES;
    static {
        CODES = new java.util.HashMap<>();
        CODES.put("OK",    200);
        CODES.put("NOT_FOUND", 404);
        CODES.put("ERROR", 500);
    }

    public static void main(String[] args) {
        double earthWeight = 75.0; // kg
        double mass = earthWeight / Planet.EARTH.surfaceGravity();

        System.out.println("Weight on each planet (for " + earthWeight + " kg person):");
        for (Planet p : Planet.values()) {
            System.out.printf("  %-10s : %6.2f N%n", p, p.surfaceWeight(mass));
        }

        System.out.println("\nHTTP status codes:");
        CODES.forEach((k, v) -> System.out.printf("  %-10s = %d%n", k, v));

        System.out.println("Max retries: " + Config.MAX_RETRIES);
        System.out.println("Earth ordinal: " + Planet.EARTH.ordinal()); // 2
    }
}