What is the difference between inner class and nested class in Java?
What is the difference between inner class and nested class in Java?
I'd be happy to help!
In Java, an inner class (also known as a local class) and a nested class are two related but distinct concepts.
A nested class is a class that is defined inside another class or interface. This means that the outer class or interface defines the namespace for the nested class, which is also referred to as an anonymous class. A nested class does not have its own package statement; instead, it inherits the package of the enclosing class or interface.
Here's an example:
public class Outer {
public static class Inner {
// code here
}
}
In this example, Inner
is a nested class within the Outer
class. The Inner
class has access to all members of the Outer
class, including its methods and variables.
On the other hand, an inner class (or local class) is a special type of nested class that is defined inside a method. This means that the inner class has a closer relationship with the method it's defined within than with any other class in the program.
Here's an example:
public class Outer {
public void myMethod() {
class Inner {
// code here
}
}
}
In this example, Inner
is an inner class that is defined inside the myMethod()
method of the Outer
class. The Inner
class has access to all members of the enclosing method, including its local variables and parameters.
The key difference between a nested class and an inner class lies in their scope:
A nested class has a broader scope than an inner class because it is defined outside any particular method. This means that a nested class can be used anywhere within the enclosing class or interface. An inner class, on the other hand, is scoped to the method within which it's defined. This means that an inner class can only be used within the same method and its subclasses.In summary, both nested classes and inner classes are forms of encapsulation in Java, but they differ in their scope and relationship with the enclosing class or interface. While a nested class is defined outside any particular method and has a broader scope, an inner class is defined inside a method and is scoped to that method alone.
Java inner class vs nested class javatpoint example
I'm glad you're interested in learning about the differences between Java inner classes and nested classes! Here's a detailed explanation, with examples from Javatpoint.
Java Inner Classes
In Java, an inner class is a class that is defined inside another class. The outer class (the one containing the inner class) has access to the inner class's private members. This means the outer class can create instances of the inner class and interact with them directly. Here's an example from Javatpoint:
class Outer {
int x = 10;
class Inner {
void display() {
System.out.println("Value of x is: " + x);
}
}
}
public class Main {
public static void main(String[] args) {
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
inner.display(); // prints the value of x
}
}
In this example, Outer
is the outer class, and Inner
is an inner class. The main
method creates an instance of the Outer
class (outer
) and then uses that instance to create an instance of the Inner
class (inner
). The display
method in the Inner
class has access to the private member x
of the Outer
class, which is why it can print its value.
Java Nested Classes
On the other hand, a nested class is a class that is defined inside another class, but unlike an inner class, a nested class does not have direct access to the outer class's members. A nested class is essentially a standalone class that happens to be defined within another class. Here's an example from Javatpoint:
class Outer {
int x = 10;
public static class Inner {
void display() {
System.out.println("Value of x is: " + x);
}
}
}
public class Main {
public static void main(String[] args) {
Outer.Inner inner = new Outer.Inner();
// error: cannot resolve symbol 'x'
// inner.display(); // compilation fails because Inner doesn't have access to x
}
}
In this example, Outer
is the outer class, and Inner
is a nested class. The main
method creates an instance of the Inner
class (inner
) using the new Outer.Inner()
syntax. However, since a nested class does not have direct access to its containing class's members, the display
method in the Inner
class cannot print the value of x
.
Key Takeaways
Inner classes have direct access to their containing class's private members. Nested classes do not have direct access to their containing class's private members. You can use inner classes as helper classes to perform tasks specific to a particular instance of an outer class. You might use nested classes when you want to create standalone classes that happen to be defined within another class.I hope this explanation helps clarify the differences between Java inner classes and nested classes!