Java static nested class

In Java, a static nested class is a nested class that is declared as static. It is a class that is declared within another class, but unlike an inner class, it is not associated with an instance of the outer class. Instead, it can be instantiated independently of any instance of the outer class.

Here is an example of how to declare a static nested class in Java:

public class OuterClass {
    
    // ...
    
    public static class StaticNestedClass {
        
        // ...
        
    }
    
}

In the above example, StaticNestedClass is a static nested class declared inside OuterClass. It can be accessed using the dot notation, like this:

OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();

Since it is a static class, it can be accessed without creating an instance of OuterClass. However, it can still access static members of OuterClass if they are declared as public or protected.

Static nested classes can be useful for organizing related classes within the same file, and can also be used to create helper classes that are not directly related to the outer class. They can also be used to encapsulate implementation details of the outer class, keeping them hidden from other classes outside of the outer class.

Java static nested class example with instance method:

Here’s an example of a static nested class in Java with an instance method:

public class OuterClass {
    
    private static String outerStaticString = "Hello, from outer class!";
    private String outerInstanceString = "Hello, from outer instance!";
    
    public static class StaticNestedClass {
        
        private String nestedString = "Hello, from nested class!";
        
        public void printStrings() {
            System.out.println(outerStaticString);
            // System.out.println(outerInstanceString); - this won't compile, because it's not a static member
            System.out.println(nestedString);
        }
        
    }
    
}

In the above example, StaticNestedClass is a static nested class that has an instance method printStrings(). This method accesses the static member outerStaticString of the outer class, but it cannot access the non-static member outerInstanceString, because it is not associated with any instance of the outer class.

To create an instance of the nested class and call the instance method, you can do this:

OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
nestedObject.printStrings();

This will output:

Hello, from outer class!
Hello, from nested class!

Note that the nested class can access private static members of the outer class, but not private non-static members.

Internal class generated by the compiler:

In Java, the compiler can generate internal classes (also known as synthetic classes or synthetic methods) to support certain language features or to implement some bytecode-level optimizations.

One example of an internal class generated by the compiler is a synthetic accessor method. These are methods that the compiler generates to provide access to private fields or methods of a class from other classes. Since private members are not accessible from outside the class in which they are declared, the compiler generates synthetic accessor methods to allow other classes to access them indirectly.

Here’s an example:

public class MyClass {
    
    private int myPrivateField;
    
    public int getMyPrivateField() {
        return myPrivateField;
    }
    
    public void setMyPrivateField(int value) {
        myPrivateField = value;
    }
    
}

When you compile this class, the compiler generates a synthetic accessor method for the myPrivateField field, which allows other classes to get and set its value indirectly:

class MyClass$1 {
    
    static int access$002(MyClass obj, int value) {
        obj.myPrivateField = value;
        return value;
    }
    
    static int access$000(MyClass obj) {
        return obj.myPrivateField;
    }
    
}

Note that the name of the internal class starts with the name of the enclosing class, followed by a $ symbol, and then a unique number to distinguish it from other synthetic classes.

Another example of an internal class generated by the compiler is a lambda class. When you use a lambda expression in your code, the compiler generates a class that implements the functional interface that corresponds to the lambda expression. This class is an internal class that is not visible to your code, but it is used by the JVM to execute the lambda expression.

List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
names.forEach(name -> System.out.println("Hello, " + name));

When you compile this code, the compiler generates a lambda class that implements the Consumer interface:

class MyClass$2 implements Consumer<String> {
    
    public void accept(String name) {
        System.out.println("Hello, " + name);
    }
    
}

This lambda class is used by the forEach method to iterate over the list of names and print a greeting for each name.

Internal classes are not part of the public API of your code, and you don’t need to know about them in order to use Java effectively. However, understanding how the compiler generates internal classes can help you debug issues in your code or optimize its performance.

Java static nested class example with a static method:

Sure, here’s an example of a Java static nested class with a static method:

public class OuterClass {
    
    private static String outerStaticString = "Hello, from outer class!";
    
    public static class StaticNestedClass {
        
        private static String nestedStaticString = "Hello, from nested class!";
        
        public static void printStrings() {
            System.out.println(outerStaticString);
            System.out.println(nestedStaticString);
        }
        
    }
    
}

In this example, StaticNestedClass is a static nested class that has a static method printStrings(). This method can access the static members of both the outer class (outerStaticString) and the nested class (nestedStaticString).

To call the static method of the nested class, you can do this:

OuterClass.StaticNestedClass.printStrings();

This will output:

Hello, from outer class!
Hello, from nested class!

Note that you don’t need to create an instance of the nested class to call its static method, since static methods are associated with the class itself rather than any instance of the class.