What is the difference between inner and nested classes in Java?

April 182 Published: 10/07/2024

What is the difference between inner and nested classes in Java?

A great question about Java!

In Java, both inner (also known as local) and nested classes are used to define a class within another class. However, there are significant differences between these two types of classes.

Inner Classes

An inner class is a class that is defined inside another class, but has access only to the members of its outer class. This means an inner class can use and manipulate only the variables and methods of its outer class, not any other class. Inner classes are often used when you want to create a utility class or a helper class that operates within the context of its outer class.

Here's an example:

public class Outer {

private int x;

public class Inner {

public void doSomething() {

System.out.println(x); // This works because Inner has access to Outer's members

}

}

public void doSomethingElse() {

new Inner().doSomething(); // This also works

}

}

In this example, the Inner class is defined within the Outer class and has access only to the x variable of the outer class.

Nested Classes

A nested class is a class that is defined inside another class, but can have its own members (methods and variables) and even be instantiated separately from its outer class. Nested classes are often used when you want to group related classes together or create a hierarchy of classes.

Here's an example:

public class Outer {

public static class Inner {

private int y;

public void doSomething() {

System.out.println(y); // This works because Inner has its own member

}

}

public static void main(String[] args) {

new Outer.Inner().doSomething(); // This also works

}

}

In this example, the Inner class is defined within the Outer class and has its own member variable y. The inner class can be instantiated separately from its outer class.

Key differences

To summarize:

Inner classes have access only to the members of their outer class, while nested classes have their own members and can be instantiated separately. Inner classes are often used for utility or helper classes, while nested classes are used for grouping related classes together or creating a hierarchy. Inner classes cannot be instantiated separately from their outer class, while nested classes can.

I hope this helps clarify the differences between inner and nested classes in Java!

How to call inner class method from outer class in Java?

I'll respond in English this time!

To call an inner class method from the outer class, you need to create an instance of the inner class and then call its methods using that instance. Here's a step-by-step guide:

Let's consider an example with an outer class OuterClass having an inner class InnerClass. The InnerClass has a method innerMethod() which we want to call from the OuterClass.

public class OuterClass {

public class InnerClass {

public void innerMethod() {

System.out.println("This is called from the inner class");

}

}

public static void main(String[] args) {

// Create an instance of the inner class

InnerClass inner = new OuterClass().new InnerClass();

// Now we can call the innerMethod of the inner class

inner.innerMethod();

}

}

In this example, InnerClass is a non-static inner class. This means that each instance of OuterClass has its own instance of InnerClass.

To create an instance of the InnerClass, you need to use the syntax new OuterClass().new InnerClass() because InnerClass is a member of OuterClass.

When you run this program, it will output:

This is called from the inner class

Now let's consider another example with a static inner class.

public class OuterClass {

public static class InnerClass {

public static void innerMethod() {

System.out.println("This is called from the static inner class");

}

}

public static void main(String[] args) {

// We can directly call the static inner method without creating an instance of the outer or inner classes

InnerClass.innerMethod();

}

}

In this case, we don't need to create an instance of OuterClass because InnerClass is a static inner class. This means that there's only one instance of InnerClass, and it's shared across all instances of OuterClass.

To call the innerMethod() from the outer class, you can use:

public class OuterClass {

public static void main(String[] args) {

InnerClass.innerMethod();

}

}

When you run this program, it will also output:

This is called from the static inner class

In conclusion, to call an inner class method from an outer class in Java, you need to create an instance of the non-static inner class (or use a static inner class). The key is to understand the difference between non-static and static inner classes.