0% found this document useful (0 votes)
1 views4 pages

4. Nested Class in Java

In Java, nested classes are defined within another class and include Static Nested Classes and Non-static Nested Classes (Inner Classes). Static Nested Classes can access only static members of the outer class, while Inner Classes can access both static and non-static members. Additionally, there are Local Inner Classes defined within methods and Anonymous Inner Classes that are unnamed and used for immediate implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views4 pages

4. Nested Class in Java

In Java, nested classes are defined within another class and include Static Nested Classes and Non-static Nested Classes (Inner Classes). Static Nested Classes can access only static members of the outer class, while Inner Classes can access both static and non-static members. Additionally, there are Local Inner Classes defined within methods and Anonymous Inner Classes that are unnamed and used for immediate implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Nested Class in Java

In Java, a nested class is a class that is defined within another class. There are two main types
of nested classes in Java:
1. Static Nested Class: A class defined inside another class with the static modifier. It can
be accessed without an instance of the outer class.
2. Non-static Nested Class (Inner Class): A class defined inside another class without
the static modifier. It requires an instance of the outer class to be created.
1. Static Nested Class
A static nested class is associated with the outer class itself, not with instances of the outer
class. Since it's a static class, it can only access the static members of the outer class.
Example:
class OuterClass {
private static String outerStaticVariable = "Outer Static Variable";

static class StaticNestedClass {


void display() {
System.out.println(outerStaticVariable); // Accessing static member of the outer class
}
}

public static void main(String[] args) {


// Creating instance of Static Nested Class
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
nestedObject.display();
}
}
2. Non-static Nested Class (Inner Class)
A non-static nested class (or Inner Class) is associated with an instance of the outer class.
Therefore, it can access both the static and non-static members of the outer class, including
private ones.
Example:
class OuterClass {
private String outerInstanceVariable = "Outer Instance Variable";

class InnerClass {
void display() {
System.out.println(outerInstanceVariable); // Accessing non-static member of the outer
class
}
}

public static void main(String[] args) {


// Creating instance of the Outer Class
OuterClass outerObject = new OuterClass();

// Creating instance of Inner Class using the outer class instance


OuterClass.InnerClass innerObject = outerObject.new InnerClass();
innerObject.display();
}
}
Types of Nested Classes
In addition to Static Nested Class and Inner Class, there are also:
• Local Inner Classes: These are classes defined inside methods or constructors.
• Anonymous Inner Classes: These are unnamed classes defined at the point of
instantiation, typically used for implementing interfaces or extending classes.
Example of Local Inner Class
class OuterClass {
void outerMethod() {
class LocalInnerClass {
void display() {
System.out.println("This is a local inner class.");
}
}
LocalInnerClass localClass = new LocalInnerClass();
localClass.display();
}

public static void main(String[] args) {


OuterClass outer = new OuterClass();
outer.outerMethod();
}
}
Example of Anonymous Inner Class
interface Greeting {
void sayHello();
}

public class Main {


public static void main(String[] args) {
Greeting greeting = new Greeting() {
public void sayHello() {
System.out.println("Hello from the anonymous inner class!");
}
};
greeting.sayHello();
}
}
Key Points:
• Static Nested Class: Can be accessed without an instance of the outer class, and can
only access static members of the outer class.
• Inner Class: Requires an instance of the outer class, and can access both static and non-
static members of the outer class.
• Local Inner Class: Defined inside a method, and can access final local variables of the
method.
• Anonymous Inner Class: An unnamed inner class used for immediate use, often
implementing an interface or extending a class.

You might also like