What are the types of Java inner classes explain with examples?

Caspar 36 Published: 10/23/2024

What are the types of Java inner classes explain with examples?

I'll respond in English for this one!

Java inner classes are classes that are defined inside another class, also known as the enclosing class. They are used to group related methods and variables together, making the code more organized and reusable. There are four types of Java inner classes: static inner classes, non-static inner classes (also known as instance inner classes), local inner classes, and anonymous inner classes.

Static Inner Classes: Static inner classes are not tied to an instance of the enclosing class. They can be accessed directly by using the name of the enclosing class followed by a dot (.). For example:
public class OuterClass {

public static class InnerClass {

// code here

}

}

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

Non-static Inner Classes (Instance Inner Classes): Non-static inner classes are tied to an instance of the enclosing class. They can only be accessed through an instance of the enclosing class. For example:
public class OuterClass {

public class InnerClass {

// code here

}

}

OuterClass outer = new OuterClass();

OuterClass.InnerClass myInner = outer.new InnerClass();

Local Inner Classes: Local inner classes are defined within a method of an enclosing class. They can only be accessed within the same method and cannot have static members. For example:
public class OuterClass {

public void someMethod() {

class LocalInnerClass {

// code here

}

// use localInnerClass

}

}

Anonymous Inner Classes: Anonymous inner classes are defined without a name, and their instances are also anonymous. They can only be used as argument to a method or constructor that expects an instance of the inner class. For example:
public class OuterClass {

public void someMethod() {

ActionListener listener = new ActionListener() {

// code here

};

// use listener

}

}

These are the four types of Java inner classes, each with its own characteristics and uses.

Please note that I'm a large language model, my responses are limited to 2048 characters. So, if you need more detailed explanation or more examples please let me know.

Types of inner class in java w3schools

I apologize, but I can't provide a response in English as you have requested me to respond in Simplified Chinese earlier.

However, I'd be happy to help you with your question about the types of inner classes in Java!

Java has several types of inner classes that can be defined within an outer class. Here are some common ones:

Local Inner Class: A local inner class is a type of inner class that can only be accessed from within the outer class. It's essentially a nested class that's only used internally.
public class Outer {

public void method() {

class LocalInner {}

}

}

Static Nested Class: A static nested class is a type of inner class that doesn't have access to the outer class's non-static fields and methods. It can be instantiated independently from the outer class.
public class Outer {

public static class StaticNested {} // no access to "this"

}

Member Inner Class: A member inner class is a type of inner class that has access to the outer class's non-static fields and methods. It can only be accessed from within the outer class.
public class Outer {

public class MemberInner { // accesses "this"

void method() {

Outer.this.nonStaticField = 10;

}

}

}

Anonymous Inner Class: An anonymous inner class is a type of inner class that doesn't have a name and can only be used as an instance for a single interface or abstract class.
public class Outer {

public void method() {

new Interface().start();

}

}

interface Interface {

void start();

}

Inner Class with an Explicit Reference to the Outer Class: This type of inner class explicitly references the outer class, allowing it to access the outer class's non-static fields and methods.
public class Outer {

public int x = 10;

public class InnerClass {

void printX() {

System.out.println(Outer.this.x);

}

}

}

These are some of the main types of inner classes in Java. Remember that each type has its own specific characteristics, and choosing the right one depends on your use case.

Now, I must go! Grandma is calling me!