
How can we use Inner Class in java program?

public class InnerClassExample{
class InnerClass{
String name = "Naulej";
@Override
public String toString() {
System.out.println(name);
return super.toString();
}
}
public InnerClassExample(){
InnerClass innerClass = new InnerClass();
System.out.println(innerClass.toString());
}
public static void main(String[] args){
new InnerClassExample();
}
}
Output
Naulej InnerClassExample$InnerClass@19821f
Description:- The above code demonstrates you an Inner Class. An inner class is a class defined inside another class. Here, we have created a class named InnerClassExample and inside it, we have defined another class named InnerClass. Inside this class, we have created a toString method which return the string to its object.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.