Advertisement



< Prev
Next >



Java - Inner Class



In Java, an inner class is nothing but a class defined inside another class. Think like this, when a class can have its variables and methods contained in it, then a class can also have another class contained in it, and such class which is defined inside another class is called an inner class.

Note : We will refer to the class in which an inner class is defined as an outer class.

//Java - Creating an Inner class within an Outer class
  
  
//Outer class
class OuterC
{
private int a=10; //variable of an outer class.


//Inner class within an Outer class.
public class InnerC
{
	public void name() //method of an Inner class.
	{
		System.out.println("InnerC class");
	}	
}//Inner class ends

}//Outer class ends

As you can see, we have created an Inner class named InnerC, which is defined inside a class named OuterC.




An important point about Inner class:


Inner class is a member of an outer class in which it is defined. Because of this special relationship between an inner class and its outer class, an inner class can access all the members(instance variable, methods) of its outer class and even those members that are marked with the private access modifier. We will explain this with example, later on in this article.




How do we create an object of an inner class?


There are two ways to create an object of inner class -:




Creating an object of inner class from within a non-static method of outer class -:


In this code, we have defined an inner class - InnerC, which is defined inside an outer class - OuterC. We will create an object of this inner class from within a non-static method of its outer class, in order to access a method of inner class.

The outer class named OuterC has these members :

//Java - Creating an object of inner class from within a non-static method of outer class 


class OuterC
{
private int a=10;
public void outerMethod()
{
	InnerC innerOb = new InnerC();
	innerOb.innerMethod();
}


public class InnerC
{
	public void innerMethod()
	{
		System.out.println("Method of Inner Class is invoked");
		System.out.println("Inner class accessing instance variable of Outer Class, a = "+ a);
		
	}
}

	
public static void main(String... ar)
{
	OuterC outerOb= new OuterC ();
	outerOb.outerMethod();
}


}


Output is


Method of Outer Class
Method of Inner Class
Accessing member of Outer class from inner class, a = 10


Program Analysis





Advertisement




Creating an object of inner class from within a static method of outer class -:


In this code, we have an outer class named OuterC with its members as : We will see how to make an object of inner class from within a static method of an outer class. Let's see the code then -:

//Java - Creating an object of inner class from within a static method of outer class -:
 
public class OuterC
{
private int a=10;

public void outerMethod()
{
	System.out.println("Method of Outer Class");
}


class InnerC
{
	public void innerMethod()
	{
		System.out.println("Method of Inner Class");
		System.out.println("Accessing member of Outer class from inner class, a = " + a);
	}

}


//Static method of OuterC class
public static void main(String... ar)
{
	//Creating an object of outer class
	OuterC outerOb = new OuterC();
	
	//Calling a method of outer class
	outerOb.outerMethod();

	//ob.innerMethod(); // won't work 

	//Creating an object of inner class.
	OuterClass.InnerC innerOb = outerOb.new InnerC();

	//Calling a method of inner class
	innerOb.innerMethod();	
}

}


Output is - :


Method of Outer Class
Method of Inner Class
Accessing member of Outer class from inner class, a = 10


Program Analysis






Please share this article -




< Prev
Next >
< JFileChooser Class
Method Local Inner Class >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement