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
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.
Static Nested Classes in Java:
public class Outer { static class StaticNested { // Static nested class } }
Java Static Inner Class vs Non-Static Inner Class:
public class Outer { static class StaticNested { /* ... */ } class NonStaticNested { /* ... */ } }
Accessing Static Inner Class Members in Java:
Outer.StaticNested staticNested = new Outer.StaticNested();
Instantiating Static Inner Classes in Java:
Outer.StaticNested instance = new Outer.StaticNested();
Java Static Inner Class Visibility:
public class AnotherClass { Outer.StaticNested staticNested = new Outer.StaticNested(); }
Static Inner Class and Encapsulation in Java:
public class Outer { private static class StaticNested { // Encapsulated static nested class } }
Java Static Inner Class Example Code:
public class Outer { static class Logger { void log(String message) { System.out.println(message); } } }
Java Outer Class and Static Inner Class Relationship:
public class Outer { static class StaticNested { // Relationship with Outer } }
Static Inner Classes for Utility in Java:
public class UtilityClass { static class Helper { static void performTask() { // Utility function } } }
Java Anonymous Static Inner Class:
public class Outer { public static void main(String[] args) { Runnable runnable = new Runnable() { public void run() { // Implementation } }; } }