< Prev
Next >



Inner Class



Inner class is nothing but a class defined inside another class. Think like this, when a class can have its member variables and methods contained in it, then a class can also have a member class contained it and this member class defined inside a class is an inner class.

Note : We will refer to the class in which an inner class defined as an "outer class". "You can even give a very simple, basic diagram here to an inner class inside an outer class."

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

Program to create an object of inner class from the inside of 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



Program to create 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 for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
JSP & Servlets

Ad2