Home » Java programming language

Explain Inner class in java



Learn: What is inner class in java? Here we will learn core concepts of java? Why inner class used in java?
Submitted by Preeti Jain, on February 06, 2018

Inner class in java

Sometime we can declare a class within another class such type of class is called inner class.

Syntax:

class OuterClass{
	class InnerClass{
	}
}

Inner class concepts introduced in 1.0 version to fix GUI bugs.

Without existing one type of object if there is no chance of existing another type of object then we should go for inner class.

Example:

In this example without existing finger class object then there is no chance of existing ring class object.

class Fingers{
	class Ring{
	}
}

Inner class is HAS-A relation (i.e. without existing outer class object then there is no chance of existing inner class object).

1) Normal Inner class

When we are declaring named or identifier defined class inside directly in another class without using a static modifier such type of class is called normal inner class.

When we compile inner class then two .class files will be made like (outer-class-name .class and outer-class-name $ inner-class-name).

Inside the inner class, we cannot declare any static variable or methods including the main method.

If we want to access inner class methods from static area of outer class then we should first create an outer class object (i.e without existing outer class object then the inner class object may not exists) then only after we can access inner class methods.

Example:

class OuterClassStaticAccess{
	
	class InnerClassStaticAccess{
		public void innerClass(){
			System.out.println("Welcome in inner class");
		}
	}
	
	public static void main(String[] args){
			OuterClassStaticAccess o =  new OuterClassStaticAccess();
			InnerClassStaticAccess i = o.new InnerClassStaticAccess();
			i.innerClass();
		}
}

Output:

D:\Java Articles>java OuterClassStaticAccess
Welcome in inner class

If we want to access inner class methods from instance area of outer class then we also should first create an outer class object(i.e without existing outer class object then the inner class object may not exists) then only after we can access inner class methods.

Example:

class OuterClassInstanceAccess{
	
	class InnerClassInstanceAccess{
		public void innerClass(){
			System.out.println("Welcome in inner class");
		}
	}

	public void outerInstanceClass(){
		InnerClassInstanceAccess i = new InnerClassInstanceAccess();
		i.innerClass();
	}

	public static void main(String[] args){
		OuterClassInstanceAccess o =  new OuterClassInstanceAccess();
		o.outerInstanceClass();
	}
}

Output

D:\Java Articles>java OuterClassInstanceAccess
Welcome in inner class

If we want to access inner class methods from outside area of outer class then we also should first create an outer class object (i.e without existing outer class object then the inner class object may not exists) then only after we can access inner class methods.

Example:

class OuterOuterClassAccess{
	
	class InnerClassAccess{
		public void innerMethod(){
			System.out.println("Welcome in inner class");
		}
	}

	public void outerMethod(){
		InnerClassAccess ica = new InnerClassAccess();
		ica.innerMethod();
	}
}

class Demo{
	public static void main(String[] args){
		OuterOuterClassAccess ooca =  new OuterOuterClassAccess();
		ooca.outerMethod();
	}
}

Output

D:\Java Articles>java Demo
Welcome in inner class





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.