Hi friends, welcome back to Java Code Online. Today's article discuss the regular inner class in Java. Though the inner classes could be classified in 4 sub types, regular inner classes is the most commonly used sub type.
The regular inner class is a full fledged member of the outer class, so it shares all the features of a member of a class. Like an Inner class has access to all the variable of the outer class including the private variables. There is a special way to instantiate an Inner class. For instantiating a regular Inner Class, you need to instantiate the Outer class first.
If you are invoking the regular inner class from inside the Outer class, then it could be instantiated like a regular class, like:-
InnerClass ic = new InnerClass();
But if the Inner class needs to be instantiated from outside the Outer class, then first you need to get a reference of the Outer class Object, and then create an Object of the Inner class from that. The syntax is something like given below:-
OuterClass oc = new Outer Class();
OuterClass.InnerClass ic = oc.new InnerClass();
I hope that this article was helpful in clearing all your doubts about Regular Inner Classes.
Types of Inner Class
1. Regular Inner Class
2. Method Local Inner Class
3. Anonymous Inner Class
4. Static Nested Inner Class
For more information on Java, keep buzzing Java Code Online.