Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java Static Inner Class

In Java, a static inner class (also known as a static nested class) is a nested class that is declared as static. Unlike non-static (regular) inner classes, static inner classes don't have access to the instance variables and methods of the enclosing class. However, they can access static members of the enclosing class. Static inner classes are useful when you want to create a helper class that's logically related to the enclosing class but doesn't need to access its instance data.

Defining a static inner class

To create a static inner class, use the static keyword before the class keyword in the nested class declaration. Here's an example:

public class OuterClass {
    private static String staticMessage = "Static message from the outer class";

    // Static inner class
    public static class StaticNestedClass {
        public void printMessage() {
            System.out.println(staticMessage);
        }
    }
}

In this example, we have an OuterClass with a static inner class called StaticNestedClass. The StaticNestedClass has a method printMessage() that can access the static member staticMessage of the OuterClass.

Creating an instance of a static inner class

To create an instance of a static inner class, you don't need an instance of the enclosing class. You can create an instance directly using the following syntax:

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

Example of using a static inner class

Here's an example of using a static inner class:

public class Main {
    public static void main(String[] args) {
        // Create an instance of the static inner class
        OuterClass.StaticNestedClass nestedInstance = new OuterClass.StaticNestedClass();

        // Call the printMessage() method of the static inner class
        nestedInstance.printMessage();
    }
}

In this example, we create an instance of the StaticNestedClass and call its printMessage() method, which prints the staticMessage of the OuterClass.

In this tutorial, we covered the basics of static inner classes in Java, including how to define a static inner class, create an instance, and use it in your code. Static inner classes are helpful when you need a logically related helper class that doesn't need access to the instance data of the enclosing class.

  1. Static Nested Classes in Java:

    • Definition of a static nested class within an outer class.
    public class Outer {
        static class StaticNested {
            // Static nested class
        }
    }
    
  2. Java Static Inner Class vs Non-Static Inner Class:

    • Comparison between static and non-static inner classes.
    public class Outer {
        static class StaticNested { /* ... */ }
        class NonStaticNested { /* ... */ }
    }
    
  3. Accessing Static Inner Class Members in Java:

    • Accessing static members of a static inner class.
    Outer.StaticNested staticNested = new Outer.StaticNested();
    
  4. Instantiating Static Inner Classes in Java:

    • Creating instances of static inner classes.
    Outer.StaticNested instance = new Outer.StaticNested();
    
  5. Java Static Inner Class Visibility:

    • Discussing visibility and access rules of static inner classes.
    public class AnotherClass {
        Outer.StaticNested staticNested = new Outer.StaticNested();
    }
    
  6. Static Inner Class and Encapsulation in Java:

    • Achieving encapsulation with a static inner class.
    public class Outer {
        private static class StaticNested {
            // Encapsulated static nested class
        }
    }
    
  7. Java Static Inner Class Example Code:

    • Demonstrating a practical example of a static inner class.
    public class Outer {
        static class Logger {
            void log(String message) {
                System.out.println(message);
            }
        }
    }
    
  8. Java Outer Class and Static Inner Class Relationship:

    • Understanding the relationship between an outer class and its static inner class.
    public class Outer {
        static class StaticNested {
            // Relationship with Outer
        }
    }
    
  9. Static Inner Classes for Utility in Java:

    • Using static inner classes for utility functions.
    public class UtilityClass {
        static class Helper {
            static void performTask() {
                // Utility function
            }
        }
    }
    
  10. Java Anonymous Static Inner Class:

    • Creating an anonymous static inner class.
    public class Outer {
        public static void main(String[] args) {
            Runnable runnable = new Runnable() {
                public void run() {
                    // Implementation
                }
            };
        }
    }