Regular inner class
In this tutorial, we are going to discuss about Regular inner class in java.If we are declaring any named class inside another class directly without static modifier such type of inner classes are called normal or regular inner classes.
A regular inner class is a nested class that is defined within another class without using the static
keyword. This means that each instance of the outer class contains an instance of the inner class. Regular inner classes have access to all members (fields and methods) of the outer class, including private members, and can be instantiated only within an instance of the outer class.
class OuterClass {
class InnerClass {
}
}
When you compile the above code, compiler generates two classes named OuterClass.class and OuterClass$InnerClass.class.
package com.ashok.innerclasses;
/**
*
* @author ashok.mariyala
*
*/
public class OuterClass {
class InnerClass {
}
public static void main(String[] args) {
System.out.println("OuterClass main method");
}
}
Output
java OuterClass
OuterClass main method
java OuterClass$InnerClass
Exception in thread "main" java.lang.NoSuchMethodError:main
Inside inner class we can’t declare static members. Hence it is not possible to declare main() method and we can’t invoke inner class directly from the command prompt.
Accessing inner class code from static area of outer class
package com.ashok.innerclasses;
/**
*
* @author ashok.mariyala
*
*/
public class OuterClass {
class InnerClass {
private void m1() {
System.out.println("Inside inner class");
}
}
public static void main(String[] args) {
OuterClass out = new OuterClass();
OuterClass.InnerClass in = out.new InnerClass();
in.m1();
// Or
OuterClass.InnerClass inn = new OuterClass().new InnerClass();
inn.m1();
// Or
new OuterClass().new InnerClass().m1();
}
}
Output
Inside inner class
Inside inner class
Inside inner class
Accessing inner class code from instance area of outer class
package com.ashok.innerclasses;
/**
*
* @author ashok.mariyala
*
*/
class OuterClass {
class InnerClass {
public void inner() {
System.out.println("Inside InnerClass class method");
}
}
public void outer() {
InnerClass in = new InnerClass();
in.inner();
}
public static void main(String[] args) {
OuterClass out = new OuterClass();
out.outer();
}
}
Output
Inside InnerClass class method
Accessing inner class code from outside of outer class
package com.ashok.innerclasses;
/**
*
* @author ashok.mariyala
*
*/
class OuterClass {
class InnerClass {
public void inner() {
System.out.println("Inside InnerClass method");
}
}
}
public class Test {
public static void main(String[] args) {
new OuterClass().new InnerClass().inner();
}
}
Output
Inside InnerClass method
From inner class we can access all members of outer class (both static and non-static, private and non private methods and variables) directly.
package com.ashok.innerclasses;
/**
*
* @author ashok.mariyala
*
*/
public class OuterClass {
String str = "Waytoeasylearn";
static int y = 100;
class InnerClass {
public void inner() {
System.out.println(str);// 10
System.out.println(y);// 20
}
}
public static void main(String[] args) {
new OuterClass().new InnerClass().inner();
}
}
Output
Waytoeasylearn
100
Within the inner class “this” always refers current inner class object. To refer current outer class object we have to use “outer class name.this”.
package com.ashok.innerclasses;
/**
*
* @author ashok.mariyala
*
*/
public class OuterClass {
int x = 10;
class InnerClass {
int x = 100;
public void inner() {
int x = 1000;
System.out.println(x);// 1000
System.out.println(this.x);// 100
System.out.println(OuterClass.this.x);// 10
}
}
public static void main(String[] args) {
new OuterClass().new InnerClass().inner();
}
}
Output
1000
100
10
The applicable modifiers for outer classes are
- public
- default
- final
- abstract
- strictfp
But for the inner classes in addition to this the following modifiers also allowed.
- private
- protected
- static [static nested classes]
Nesting of Inner classes
We can declare an inner class inside another inner class.
package com.ashok.innerclasses;
/**
*
* @author ashok.mariyala
*
*/
public class OuterClass {
class A {
class B {
private void method() {
System.out.println("Welcome to Waytoeasylearn");
}
}
}
public static void main(String[] args) {
OuterClass out = new OuterClass();
OuterClass.A a = out.new A();
OuterClass.A.B b = a.new B();
b.method();
}
}
Output
Welcome to Waytoeasylearn
That’s all about the Regular Inner Class Introduction in java. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!!