⬅ Previous Topic
Encapsulation in JavaNext Topic ⮕
Java Nested Static Class⬅ Previous Topic
Encapsulation in JavaNext Topic ⮕
Java Nested Static ClassIn Java, it's possible to define a class within another class. These are called nested classes. Think of it as a way to logically group classes that are only used in one place, helping keep your code organized and readable.
Nesting also allows you to access the members of the outer class, which can lead to more concise and tightly coupled code which is useful when designing object-oriented solutions that mirror real-world relationships.
Java provides four types of nested classes:
An inner class is a non-static nested class. It has access to all the members (including private) of the outer class.
class Outer {
private String msg = "Hello from Outer";
class Inner {
void showMessage() {
System.out.println(msg);
}
}
void demo() {
Inner inner = new Inner();
inner.showMessage();
}
}
public class Main {
public static void main(String[] args) {
Outer outer = new Outer();
outer.demo();
}
}
Hello from Outer
Here, the Inner
class can directly access the private field msg
of the Outer
class. This is a strong example of encapsulation at work.
This class is defined with the static
keyword. Unlike inner classes, it cannot access non-static members of the outer class directly.
class Container {
static int data = 30;
static class StaticInner {
void display() {
System.out.println("Data is: " + data);
}
}
}
public class Main {
public static void main(String[] args) {
Container.StaticInner obj = new Container.StaticInner();
obj.display();
}
}
Data is: 30
Because StaticInner
is static, it can be instantiated without creating an instance of the outer class. But it can only access static
members of the outer class.
These are classes defined within a method. They are not visible outside the method and are useful for encapsulating helper functionality.
class LocalDemo {
void process() {
class Helper {
void help() {
System.out.println("Helping from inside the method");
}
}
Helper helper = new Helper();
helper.help();
}
}
public class Main {
public static void main(String[] args) {
new LocalDemo().process();
}
}
Helping from inside the method
The class Helper
lives inside the method process
. It's short-lived and only exists for that method's lifecycle — ideal when you want temporary behavior tightly coupled with the method logic.
An anonymous inner class is a class without a name, used to instantiate classes or interfaces on the fly — most commonly used for implementing interfaces or abstract classes.
abstract class Greeting {
abstract void sayHello();
}
public class Main {
public static void main(String[] args) {
Greeting g = new Greeting() {
void sayHello() {
System.out.println("Hello from anonymous class!");
}
};
g.sayHello();
}
}
Hello from anonymous class!
Here, instead of defining a separate subclass for Greeting
, we define and instantiate it in one go. It’s perfect when you need a one-time implementation.
Nested classes in Java organize code in a more meaningful way. From tightly coupled inner classes to ad-hoc anonymous implementations, they offer multiple flavors of encapsulation.
class Outer {
private String msg = "Hello from Outer";
class Inner {
void showMessage() {
System.out.println(msg);
}
}
void demo() {
Inner inner = new Inner();
inner.showMessage();
}
}
public class Main {
public static void main(String[] args) {
Outer outer = new Outer();
outer.demo();
}
}
⬅ Previous Topic
Encapsulation in JavaNext Topic ⮕
Java Nested Static ClassYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.