In Java, a class can be defined within another class, such a class is called a nested class. Nested classes are divided into two categories: static and non-static. Let’s take a look at each type of nested class in more detail, with examples.
Static Nested Class
Static nested classes, also known as static inner classes, are classes that are defined within another class and are marked as static. They are similar to top-level classes in that they can have their own fields and methods, and they can be accessed using the name of the class. Following is an example of a static nested class:
public class Outer {
// Fields and methods of the outer class
static class Nested {
// Fields and methods of the nested class
}
}
To access a static nested class, we use the name of the outer class followed by the name of the nested class, like this:
Outer.Nested nested = new Outer.Nested();
Note that we do not need to create an instance of the outer class to access the nested class.
The complete example code is given below:
public class Outer {
static class Nested {
void printHello() {
System.out.println("Hello!");
}
}
public static void main(String[] args) {
Outer.Nested nested = new Outer.Nested();
nested.printHello();
}
}
The output of the above code is:
Hello!
Non-Static Inner Class (Inner Classes)
Non-static inner classes, also known as inner classes, are classes that are defined within another class but are not marked as static. They are associated with an instance of the outer class, which means that they have access to all the fields and methods of the outer class. Following is an example of an inner class:
public class Outer {
// Fields and methods of the outer class
class Inner {
// Fields and methods of the inner class
}
}
To create an instance of an inner class, we first need to create an instance of the outer class, and then use it to create an instance of the inner class, like this:
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
The complete example code is given below:
public class Outer {
class Inner {
void printHello() {
System.out.println("Hello!");
}
}
public static void main(String[] args) {
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
inner.printHello();
}
}
The output of the above code is:
Hello!
Member Inner Class
Member, inner classes are inner classes that are defined as a member of the outer class, just like fields and methods. They can be either static or non-static. Following is an example of a non-static member inner class:
public class Outer {
// Fields and methods of the outer class
class Inner {
// Fields and methods of the inner class
}
}
And following is an example of a static member inner class:
public class Outer {
// Fields and methods of the outer class
static class Inner {
// Fields and methods of the inner class
}
}
Local Inner Class
Local inner classes are inner classes that are defined within a block of code, such as a method or a constructor. They can be either static or non-static.
Non-Static Local Inner Class
Following is an example of a non-static local inner class:
public class Outer {
// Fields and methods of the outer class
public void someMethod() {
class LocalInner {
// Fields and methods of the local inner class
}
}
}
The complete example code and output of the local inner non-static class is given below:
Example:
public class Outer {
class Inner {
void innerClassMethod() {
class LocalInner {
// Fields and methods of the local inner class
void printHello() {
System.out.println("Hello from Local Inner Class method!");
}
}
LocalInner localInner = new LocalInner();
localInner.printHello();
}
}
public static void main(String[] args) {
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
inner.innerClassMethod();
}
}
Output:
Hello from Local Inner Class method!
Static Local Inner Class
And following is an example of a static local inner class:
public class Outer {
// Fields and methods of the outer class
public void someMethod() {
static class LocalInner {
// Fields and methods of the local inner class
}
}
}
Anonymous Inner Class
Anonymous inner classes are inner classes that are defined and instantiated in a single statement, and they don’t have a name. They are often used as a way to implement interfaces or extend classes with a small amount of code. Following is an example of an anonymous inner class that implements an interface:
public class Outer {
// Fields and methods of the outer class
public void someMethod() {
SomeInterface obj = new SomeInterface() {
// Implementation of the interface
};
}
}
And following is an example of an anonymous inner class that extends a class:
public class Outer {
// Fields and methods of the outer class
public void someMethod() {
SomeClass obj = new SomeClass() {
// Overridden methods and additional fields and methods
};
}
}
Conclusion
Nested and inner classes in Java provide a way to define classes within the context of another class, which can be useful in certain situations. There are four types of inner classes: member inner classes, local inner classes, anonymous inner classes, and static nested classes. It’s important to use inner classes wisely, as they can make the code more complex if used excessively.
Leave a Reply