< Prev
Next >



Inner Class



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 a class contained in it and such class defined inside another class is an inner class.

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

//Creating an Inner class withint 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 defined inside a class namedOuterC.




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 private access modifier. We will explain it with the code, later on 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 the inside of non-static method of its outer class, in order to access method of inner class.

The outer class named OuterC has these members :

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







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 -:

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);
	}

}


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

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

OuterClass.InnerClass innerOb = outerOb.new InnerClass(); //Creating an object of inner class.
innerOb.innerMethod();	//calling method of inner class

}

}


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 -

Facebook Google Pinterest Reddit Tumblr Twitter



< Prev
Next >
< JFileChooser Class
Method Local Inner Class >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2