Java Inner Classes

There are four major types of Inner Classes:

1. Inside a class
2. Inside the method
3. Anonymous class
4. Static Inner class

Inside a class

Inner classes can access member variables of Outer class and vice versa. Inner classes are like members of the outer class. So, to instantiate an inner class, you need to instantiate outer class first and then use the object of the outer class to instantiate the inner class like below.

Outer o = new Outer();
Outer.Inner oi = o.new Inner();

OR

Outer.Inner oi = new Outer().new Inner();

The complete example:


package com.noor.innerclasses;

class Outer {
	private int x = 20;

	class Inner {
		private int y =10;
		public void getValue() {
			display();
			//Inner class can access private and all member variables and methods of outer class
			System.out.println("Outer.x="+x);
		}

              public void display() {
                          System.out.println("Inside inner display method");
                 }
	}

	public void display() {
		System.out.println("Inside outer display method");
	}

	public void accessInner() {

		Inner in = new Inner();
		//Outer class can access private and all member variables and methods of inner class but through the inner class object
		System.out.println("Inner.y="+in.y);
	}

}

public class InnerClassWithinClassExample {
	public static void main(String[] args) {

		Outer o = new Outer();
		Outer.Inner oi;

		//To instantiate inner class object, consider inner class as a variable inside of outer class

		oi = o.new Inner();

		//Now you can access the non private members of inner class also

		oi.getValue();

	}
}

Access modifiers for inner classes within classes are: private, public, protected, abstract, final and static. An outer class can have the above access modifiers except private and protected

Inside a method

We can have inner classes inside the method. Inner classes inside the method can only be instantiated and accesses within the method only. Inner classes within a method have access to the variables in outer class but not to variables of the method unless that variable is declared as final (constant). The reason behind this is we want the variable to stay and not just of off the stack when the method invocation completes because we never know when the inner class will access it. Hence the compiler makes this check.

Access modifiers for inner classes within methods are: abstract and final. Since inner classes act as a variable inside the method, all the rules that apply to variables of methods, apply to inner classes inside the method.

package com.noor.innerclasses;

class Outer1{
	
	private int x = 10;
	
	public void myMethod() {
		
		final int y = 20;
		
		class InnerClass {
			 
			private int z;
			
			public void getValue() {
				//Outer class variables are accessible in inner class
				System.out.println("Outer class x = "+x);
				//Enclosing method variables are accessible only when they are declared as final
				System.out.println("Enclosing method y ="+y);
			}
			
		}
		
		//Inner class within method is accessible inside the method only
		new InnerClass().getValue();		
	}
}


public class InnerClassWithinMethodExample {
	public static void main(String[] args) {
		new Outer1().myMethod();
	}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s