It is possible to define a class within another class in Java, and these classes are referred to as nested classes. They allow you to logically group classes that are only used in one place, increasing encapsulation and making code more readable and maintainable.
Syntax:
class OuterClass
{
...
class NestedClass
{
...
}
}
Note:
- The scope of the enclosing class limits a nested class’s scope. As a result, class NestedClass does not exist independently of class OuterClass in the example above.
- A nested class has access to the members of the class in which it is nested, including private members. The enclosing class can also access the members of the nested class and vice versa.
- The enclosing class is also a member of the nested class.
- A nested class can be declared private, public, protected, or package-private as a member of its enclosing class (default).
Need of Java Nested class:
Users may need to program a class in such a way that it cannot be accessed by any other class. As a result, it would be preferable if you included it in other classes.
It is easier to nest that class inside the outer class if all the class objects are part of the outer object. The outer class can then access all the inner class’s objects.
Types of Nested class:
The following are the two types of nested classes:
- nested static class: Static nested classes are nested classes that are declared static.
- inner class: A non-static nested class is an inner class.
Static Nested Class:
- A static class, also known as a static nested class in Java, is a class that is created inside another class.
- It is unable to access data members and methods that are not static.
- It can be found by looking up the name of the outer class.
- Furthermore, it has access to the outer class’s static data members, including private.
- The non-static (instance) data members or the non-static (nested) class cannot be accessed by the static nested class.
Accessing:
OuterClass.StaticNestedClass
Example:
// Java program to demonstrate accessing
// a static nested class
// outer class
class OuterClass
{
// static member
static int outer_x = 10;
// instance(non-static) member
int outer_y = 20;
// private member
private static int outer_private = 30;
// static nested class
static class StaticNestedClass
{
void display()
{
// can access static member of outer class
System.out.println("outer_x = " + outer_x);
System.out.println("outer_private = " + outer_private);
// The following statement will give compilation error
// as static nested class cannot directly access non-static members
// System.out.println("outer_y = " + outer_y);
}
}
}
// Driver class
public class StaticNestedClassDemo
{
public static void main(String[] args)
{
// accessing a static nested class
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
nestedObject.display();
}
}
Output:
outer_x = 10
outer_private = 30
Inner Class:
To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
There are two special kinds of inner classes :
- Local inner classes
- Anonymous inner classes
Example: inner class
class i.e., created inside a method, is called local inner class in java.
// Java program to demonstrate accessing
// a inner class
// outer class
class OuterClass
{
// static member
static int outer_x = 10;
// instance(non-static) member
int outer_y = 20;
// private member
private int outer_private = 30;
// inner class
class InnerClass
{
void display()
{
// can access static member of outer class
System.out.println("outer_x = " + outer_x);
// can also access non-static member of outer class
System.out.println("outer_y = " + outer_y);
// can also access a private member of the outer class
System.out.println("outer_private = " + outer_private);
}
}
}
// Driver class
public class InnerClassDemo
{
public static void main(String[] args)
{
// accessing an inner class
OuterClass outerObject = new OuterClass();
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
innerObject.display();
}
}
Output:
outer_x = 10
outer_y = 20
outer_private = 30
Example: Anonymous class
A class without any name is called an Anonymous class.
// Java program to demonstrate Need for
// Anonymous Inner class
// Interface
interface Age {
// Defining variables and methods
int x = 21;
void getAge();
}
// Class 1
// Helper class implementing methods of Age Interface
class MyClass implements Age {
// Overriding getAge() method
@Override public void getAge()
{
// Print statement
System.out.print("Age is " + x);
}
}
// Class 2
// Main class
// AnonymousDemo
class GFG {
// Main driver method
public static void main(String[] args)
{
// Class 1 is implementation class of Age interface
MyClass obj = new MyClass();
// calling getage() method implemented at Class1
// inside main() method
obj.getAge();
}
}
Output:
Age is 21
Note: also read about the Interface vs Abstract class
Follow Me
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Leave a Comment