Java Static Classes: Understanding Static Inner Classes
Learn about static classes in Java, specifically static inner classes, which are designed to provide functionality closely tied to the outer class. Discover how static inner classes work and their advantages in Java programming.
Java - Static Classes
In Java, the concept of a static class is introduced within the context of inner classes. Static inner classes are specially designed to provide functionality closely tied to the outer class.
Java Static Classes
Static classes in Java are only allowed for inner classes. A static outer class is not allowed, meaning the static
keyword cannot be used with outer classes. Static classes are defined similarly to other inner classes in Java, but with the static
keyword in front of their name. These classes possess unique characteristics that differentiate them from non-static inner classes.
Features of Java Static Classes
- No instance of outer class required: A static inner class can be instantiated without creating an instance of the outer class.
- Access to static members only: A static inner class can access the static members (variables/methods) of the outer class but does not have access to instance variables or methods of the outer class.
Syntax of Java Static Class
The syntax for declaring a static nested class is as follows:
class MyOuter {
static class Nested_Demo {
}
}
Instantiating a static nested class differs from instantiating a non-static inner class. The examples below demonstrate various ways to use a static nested class.
Example of Java Static Class
In this example, we have created a class Outer
with a static inner class NestedDemo
. In the main()
method, the static method of the inner class is called directly, as the main()
method is part of the Outer
class.
package com.example;
public class Outer {
static class NestedDemo {
public static void print() {
System.out.println("This is my nested class");
}
}
public static void main(String args[]) {
NestedDemo.print(); // Calling the static method directly
}
}
Output
This is my nested class
Java Static Class: More Examples
Example 1: Static Method from Another Class
In this example, the main()
method is in a different class. The static inner class NestedDemo
is accessed using the Outer
class, demonstrating how static inner classes can be referenced externally.
package com.example;
public class Tester {
public static void main(String[] arguments) {
Outer.NestedDemo.print(); // Accessing static inner class via Outer class
}
}
class Outer {
static class NestedDemo {
public static void print() {
System.out.println("This is my nested class");
}
}
}
Output
This is my nested class
Example 2: Instance Method in Static Class
In this example, the static inner class NestedDemo
contains an instance method print()
. The main()
method is in a different class, and we instantiate the static inner class using the Outer
class to call the instance method.
package com.example;
public class Tester {
public static void main(String[] arguments) {
new Outer.NestedDemo().print(); // Instantiating static inner class
}
}
class Outer {
static class NestedDemo {
public void print() {
System.out.println("This is my nested class");
}
}
}
Output
This is my nested class