Difference between revisions of "Inner Classes"

From Suhrid.net Wiki
Jump to navigationJump to search
Line 2: Line 2:
 
* Think of the Inner class as sort of a MEMBER of the outer class.
 
* Think of the Inner class as sort of a MEMBER of the outer class.
  
* '''Regular inner classes'''
+
<u> '''Regular inner classes''' </u>
 
* This means that  regular Inner classes '''cannot have any statics''' (variables or methods)
 
* This means that  regular Inner classes '''cannot have any statics''' (variables or methods)
 
* Only way to access an instance of the inner classes is through an instance of the outer class.
 
* Only way to access an instance of the inner classes is through an instance of the outer class.
Line 82: Line 82:
  
  
* '''Method Local Inner Classes'''
+
<u> '''Method Local Inner Classes''' </u>
 
* Class defined inside a method
 
* Class defined inside a method
 
* The class '''cannot''' use the local variables of the method, including the method arguments unless the variables are declared as final.
 
* The class '''cannot''' use the local variables of the method, including the method arguments unless the variables are declared as final.
 +
* Why ? References to the class object can continue to live on the heap, even after the method is popped off from the stack.
 +
*
  
  
 
[[Category:OCPJP]]
 
[[Category:OCPJP]]

Revision as of 23:21, 4 June 2011

  • Inner classes have access to members of the outside classes.
  • Think of the Inner class as sort of a MEMBER of the outer class.

Regular inner classes

  • This means that regular Inner classes cannot have any statics (variables or methods)
  • Only way to access an instance of the inner classes is through an instance of the outer class.
  • Remember the funny syntax:
class Outer {
  class Inner() {

  }
}

Outer o = new Outer();
Outer.Inner i =  o.new Inner()
  • There are different ways on how regular inner class instances can be created, depending on where it is accessed from. See below:
class Outer {
	private String s = "";
	
	Outer() {
		
	}
	
	Outer(String s) {
		this.s = s;
	}
	
	public String toString() {
		return s;
	}
	
	class Inner {
		private String s = "";
		
		Inner() {
			
		}
		
		Inner(String s) {
			this.s  = s;
		}
		
		public String toString() {
			return s;
		}
		
		void in() {
			System.out.println("Inner object : " + this);       //The inner objects instance
			System.out.println("Outer object : " + Outer.this); //Access the outer object's instance
		}
		
		void makeInner() {
			Inner i = new Inner(); //Within the inner class, so regular way to access.
		}
	}
	
	private void makeInner() {
		Outer o = new Outer();        //Accessing from within the outer class
		Inner i = o. new Inner();     //Class name can be used as-is
	}
	
}

public class TestInner {
	
	public static void main(String[] args) {
		Outer o1 = new Outer("o1");              //Accessing from outside the outerclass
		Outer.Inner o1i1 = o1.new Inner("o1i1"); //Class name has to be Outer.Inner 
		o1i1.in();
	}
	
}


Method Local Inner Classes

  • Class defined inside a method
  • The class cannot use the local variables of the method, including the method arguments unless the variables are declared as final.
  • Why ? References to the class object can continue to live on the heap, even after the method is popped off from the stack.