Difference between revisions of "Inner Classes"

From Suhrid.net Wiki
Jump to navigationJump to search
Line 17: Line 17:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
* There are different ways on how Inner class instances can be created, depending on where it is accessed from. See below:
 +
 +
<syntaxhighlight lang="java5">
 +
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();
 +
}
 +
 +
}
 +
 +
</syntaxhighlight>
  
 
[[Category:OCPJP]]
 
[[Category:OCPJP]]

Revision as of 03:04, 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.
  • 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 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();
	}
	
}