Explanation of Nested Classes in Java
Hello everyone, In this article we are going to talk about Nested classes in Java and we will be able to create classes inside other classes.Let's get started.
What is Nested Class?
Nested classes in Java demonstrates the classes which are defined within another class. These classes can be both static or non-static. The main advantage of nested classes is that they provide a way to logically group classes that are only used in same place, so we can increase the encapsulation and reduce complexity in the code. In this article, we will see the types of nested classes and their usages.
Static nested classes
Static nested classes are declared as static and can be accessed using the enclosing class name. They do not have access to the enclosing class's instance variables and methods if they are not declared as static.
Below you can see an example of Static Nested Class
class OuterClass {
private static int value = 10;
public static class StaticNestedClass {
public void printVal() {
System.out.println("Outer Class value is: " + value);
}
}
}
class TheExample {
public static void main(String[] args) {
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
nestedObject.printVal();
}
}
Program output will be like below
Outer Class value is: 10
Non-static nested classes
Non-static nested classes, also known as inner classes, Non- static nested classes declared without the static keyword. They have access to the enclosing class's instance variables and methods.
Below you can see example for non-static nested class
class OuterClass {
public OuterClass(){
System.out.println("Outer class initialized: ");
}
public class InnerClass {
public InnerClass(){
System.out.println("Outer class initialized: ");
}
public void greet() {
System.out.println("Inner class greeting");
}
}
}
class TheExample {
public static void main(String[] args) {
OuterClass o = new OuterClass();
OuterClass.InnerClass innerObject = o.new InnerClass();
innerObject.greet();
}
}
Program output will be like
Outer class initialized:
Outer class initialized:
Inner class greeting
Local classes
Local classes are defined within a block of code, such as a method or a constructor. They have access to the local variables and parameters of the block they are declared in, as well as the instance variables of the enclosing class.
Below you can see example of Local Nested Class
class TheExample {
public static void outerMethod() {
System.out.println("Outer method is called...");
class LocalClass {
public int localVariable = 10;
public LocalClass()
{
System.out.println("Local Class initialized...");
}
public void greet() {
System.out.println("The value of the local variable is: " + localVariable);
}
}
LocalClass localObject = new LocalClass();
localObject.greet();
}
public static void main(String[] args) {
outerMethod();
}
}
Program output will be like below
Outer method is called...
Local Class initialized...
The value of the local variable is: 10
Anonymous classes
Anonymous classes are defined without a name and are used to create one-time-use objects. They are typically used for implementing interfaces or extending classes in a single statement.
Anonymous Nested Class with Interface example
interface Vehicle {
public void start();
}
class AnonymousClass {
public void initializeClass() {
// anonymous class implementing interface
Vehicle v = new Vehicle() {
public void start() {
System.out.println("Inside an anonymous class.");
}
};
v.start();
}
}
class Main {
public static void main(String[] args) {
AnonymousClass ac = new AnonymousClass();
ac.initializeClass();
}
}
Anonymous Nested Class example with extending a Class
class Vehicle {
public void start() {
System.out.println("Vehicle is started");
}
}
class AnonymousClass {
public void initializeClass() {
// creation of anonymous class extending class Polygon
Vehicle v = new Vehicle() {
public void start() {
System.out.println("Inside an anonymous class.");
}
};
v.start();
}
}
class Main {
public static void main(String[] args) {
AnonymousClass ac = new AnonymousClass();
ac.initializeClass();
}
}
Burak Hamdi TUFAN
Comments