Compose by
Adil Mehmood
Adilm717@gmail.com
What is Inner classes
Top-Level (or Outer) Class
You can put a class inside an another class.
 A class that contains other classes is a TLC.
Classe that you can write inside another class. Common applications include
iterators and GUIs
Syantax:
class Outer{
class Inner{ }
}
note:
there is no top level static class in java unlike C#
there is only inner static class
Nested Class
Nested class:
• Class declared inside another class.
Two kinds of nested classes:
• Member class: class declared at the
member-level of a TLC.
• Local class: class declared inside a
method, constructor, or initializer block
Inner Class
Inner class(IC) refers to two special kinds of nested class:
• Non-static member class (member class with no staticmodifier).
• Local class inside a non-static member of a TLC.
Why called inner class?
• Because an object made from the class will containa reference to the
TLC.
• Use TLC.this.memberfrom inside inner class to access member of
TLC.
Restrictions:
• Inner class fields can be static, but then must also be final.
• No staticmethods or other inner classes (same for other members?)
• See language references for even more details
Handy way to think of inner
classes inside a TLC
At the member level:
- just like a variable or method.
- called member class.
At the statement level:
- just like a statement in a method
- called local class
At the expression level:
- just like an expression
- called anonymous class
Member Class (Member Level)
Rules
Structure:
public class OuterClass{
tlc_members
public class InnerClass{
mc_members
}
}
When to use?
• The inner class generates objects used specifically by TLC.
• The inner class is associated with, or “connected to,” the TLC
How does visibility work?
• The inner class can be public, private, protected,
or package.
• Instances of the inner class type have
access to all members of the outer class
(including private and static members).
Some restrictions:
• Cannot have same name as TLC or package (not
that you would want to!).
• Cannot contain static members; can have static
finalfields (constants).
How do you use a member
class?
OuterClass oref = new OuterClass();
OuterClass.InnerClass iref = oref.new
InnerClass()
iref.doSomething();
new OuterClass().new InnerClass();
• Not valid:
InnerClass iref = new InnerClass();
iref.doSomething();
Internal references with this:
• Inside inner class, the this refers to current
instance of the inner class.
• To get to current instance of TLC, save the
TLC’s this as field in the TLC or
simply use TLC.this.
public class MemberClass {
public static void main(String[] args) {
// one way:
OC a = new OC();
OC.IC b = a.new IC();
b.print(); // outputs 3
// another way:
new OC().new IC().print(); // outputs 3
}
}
class OC {
private int x = 1;
public class IC {
private int y = 2;
public void print() {System.out.println(x+y);}
}
}
Local Classes (Statement Level)
Local class location:
• Statement level declaration.
• Usually written in methods. See also constructors
and initializers.
Scope:
• Local to block.
• Can access all members of the TLC.
• Actually, things can get confusing here!
- An object of local class might persist after
method ends.
- Java does have rules for dealing with the matter
More restrictions:
• Cannot be used outside of block.
• No modifiers.
• Enclosing block’s variables must be finalfor local
class to access.
• No static, but can have static final(constants).
• Terminate with a semicolon! The class is
effectively an expression statement.
• Cannot have same name of TLC
public class LocalClass {
public static void main(String[] args) {
new OC().print();
}
}
class OC {
public void print() {
final String s = "test: ";
class Point {
private int x;
private int y;
public Point(int x,int y) { this.x=x; this.y=y; }
public String toString() { return s+"("+x+","+y+")"; }
};
System.out.println(new Point(1,2));
} // method print
} //
Anonymous Class
Location and structure:
• Defined and created at expressionlevel.
• So, has no name and no modifiers.
Syntax:
new classname( argumentlist) { classbody}
new interfacename( argumentlist) { classbody}
Adapter class:
• Adapter class defines code that another object invokes.
• Common in GUIs and iterators.
Some restrictions:
• No modifiers.
• No static, but can have static final(constants).
• No constructors, but can use initializers for samepurpose! (See Section 1.2.)
When to use?
• Class has very short body.
• Only one instance of class needed.
• Class used right after defined; no need to create new class
In example below, we print a Pointagain. But, we cannot say new Point, because we
have not defined a Pointclass. Instead, I use a placeholder, class Object. You will often
find yourself using interface names instead.
public class AnonymousClass {
public static void main(String[] args) {
new OC().print();
}
}
class OC {
public void print() {
final String s = "test: ";
System.out.println(new Object() {
private int x=1;
private int y=2;
public String toString() { return s+"("+x+","+y+")"; }
} );
}
Instantiating an Inner class
Instantiating an Inner Class from within Outer
classes
code:
class Outer{!
private int x = 7;!
public void makeInner(){!
Inner x = new Inner();!
x.seeOuter();!
}!
class Inner{!
public void seeOuter(){!
System.out.println(x);!
}!
}!
}!
5
Creating an Inner Class from
Outside of the Outer class
You have to have instance of the
Outer-class
–  Outer outer = new Outer();
  After that, you create the Inner object
–  Outer.Inner inner = outer.new Inner() ;
• One Liner:
–  Outer.Inner inner = (new Outer()).new
Inner() ;
Method-local Inner Classes
Class inside a method
•  Can be instantiated only within the
method (below the class)
•  Can use Outer classes private
members
•  Cannot use methods variables!!
–  Unless the variable is final...
class Outer{!
private int x = 7;!
public void method(){!
final String y = "hi!";!
String z = "hi!";!
class Inner{!
public void seeOuter(){!
System.out.println(x); // works!!
System.out.println(y); // works!
//System.out.println(z); // doesn't work!
}!
}!
Inner object = new Inner();!
object.seeOuter();!
}!
}!

Inner classes ,annoumous and outer classes in java

  • 1.
  • 2.
    What is Innerclasses Top-Level (or Outer) Class You can put a class inside an another class.  A class that contains other classes is a TLC. Classe that you can write inside another class. Common applications include iterators and GUIs Syantax: class Outer{ class Inner{ } } note: there is no top level static class in java unlike C# there is only inner static class
  • 3.
    Nested Class Nested class: •Class declared inside another class. Two kinds of nested classes: • Member class: class declared at the member-level of a TLC. • Local class: class declared inside a method, constructor, or initializer block
  • 4.
    Inner Class Inner class(IC)refers to two special kinds of nested class: • Non-static member class (member class with no staticmodifier). • Local class inside a non-static member of a TLC. Why called inner class? • Because an object made from the class will containa reference to the TLC. • Use TLC.this.memberfrom inside inner class to access member of TLC. Restrictions: • Inner class fields can be static, but then must also be final. • No staticmethods or other inner classes (same for other members?) • See language references for even more details
  • 5.
    Handy way tothink of inner classes inside a TLC At the member level: - just like a variable or method. - called member class. At the statement level: - just like a statement in a method - called local class At the expression level: - just like an expression - called anonymous class
  • 6.
    Member Class (MemberLevel) Rules Structure: public class OuterClass{ tlc_members public class InnerClass{ mc_members } } When to use? • The inner class generates objects used specifically by TLC. • The inner class is associated with, or “connected to,” the TLC
  • 7.
    How does visibilitywork? • The inner class can be public, private, protected, or package. • Instances of the inner class type have access to all members of the outer class (including private and static members). Some restrictions: • Cannot have same name as TLC or package (not that you would want to!). • Cannot contain static members; can have static finalfields (constants).
  • 8.
    How do youuse a member class? OuterClass oref = new OuterClass(); OuterClass.InnerClass iref = oref.new InnerClass() iref.doSomething(); new OuterClass().new InnerClass(); • Not valid: InnerClass iref = new InnerClass(); iref.doSomething();
  • 9.
    Internal references withthis: • Inside inner class, the this refers to current instance of the inner class. • To get to current instance of TLC, save the TLC’s this as field in the TLC or simply use TLC.this.
  • 10.
    public class MemberClass{ public static void main(String[] args) { // one way: OC a = new OC(); OC.IC b = a.new IC(); b.print(); // outputs 3 // another way: new OC().new IC().print(); // outputs 3 } } class OC { private int x = 1; public class IC { private int y = 2; public void print() {System.out.println(x+y);} } }
  • 11.
    Local Classes (StatementLevel) Local class location: • Statement level declaration. • Usually written in methods. See also constructors and initializers. Scope: • Local to block. • Can access all members of the TLC. • Actually, things can get confusing here! - An object of local class might persist after method ends. - Java does have rules for dealing with the matter
  • 12.
    More restrictions: • Cannotbe used outside of block. • No modifiers. • Enclosing block’s variables must be finalfor local class to access. • No static, but can have static final(constants). • Terminate with a semicolon! The class is effectively an expression statement. • Cannot have same name of TLC
  • 13.
    public class LocalClass{ public static void main(String[] args) { new OC().print(); } } class OC { public void print() { final String s = "test: "; class Point { private int x; private int y; public Point(int x,int y) { this.x=x; this.y=y; } public String toString() { return s+"("+x+","+y+")"; } }; System.out.println(new Point(1,2)); } // method print } //
  • 14.
    Anonymous Class Location andstructure: • Defined and created at expressionlevel. • So, has no name and no modifiers. Syntax: new classname( argumentlist) { classbody} new interfacename( argumentlist) { classbody} Adapter class: • Adapter class defines code that another object invokes. • Common in GUIs and iterators. Some restrictions: • No modifiers. • No static, but can have static final(constants). • No constructors, but can use initializers for samepurpose! (See Section 1.2.) When to use? • Class has very short body. • Only one instance of class needed. • Class used right after defined; no need to create new class
  • 15.
    In example below,we print a Pointagain. But, we cannot say new Point, because we have not defined a Pointclass. Instead, I use a placeholder, class Object. You will often find yourself using interface names instead. public class AnonymousClass { public static void main(String[] args) { new OC().print(); } } class OC { public void print() { final String s = "test: "; System.out.println(new Object() { private int x=1; private int y=2; public String toString() { return s+"("+x+","+y+")"; } } ); }
  • 16.
    Instantiating an Innerclass Instantiating an Inner Class from within Outer classes code: class Outer{! private int x = 7;! public void makeInner(){! Inner x = new Inner();! x.seeOuter();! }! class Inner{! public void seeOuter(){! System.out.println(x);! }! }! }! 5
  • 17.
    Creating an InnerClass from Outside of the Outer class You have to have instance of the Outer-class –  Outer outer = new Outer();   After that, you create the Inner object –  Outer.Inner inner = outer.new Inner() ; • One Liner: –  Outer.Inner inner = (new Outer()).new Inner() ;
  • 18.
    Method-local Inner Classes Classinside a method •  Can be instantiated only within the method (below the class) •  Can use Outer classes private members •  Cannot use methods variables!! –  Unless the variable is final...
  • 19.
    class Outer{! private intx = 7;! public void method(){! final String y = "hi!";! String z = "hi!";! class Inner{! public void seeOuter(){! System.out.println(x); // works!! System.out.println(y); // works! //System.out.println(z); // doesn't work! }! }! Inner object = new Inner();! object.seeOuter();! }! }!