Additional Notes Inner Classes ------------------------------------------------------------------------------- 1) General Class Structure modifiers class name extends classes implements interfaces { fields constructors (not really members) methods ------------------------------------------- innerclasses initializers innerinterfaces (yes, it seems you can) } ------------------------------------------------------------------------------- 2) Initializers + member-level statements to assign values + essentially, control structures that exist with no method or constructor see Initializers.java ------------------------------------------------------------------------------- 3) Top-level/Outer Class (TLC) + you can put a class inside an another class (INNERCLASS) + the class that contains other classes is the TLC + the TLC is declared outside of other classes/interfaces + two things to develop for ICs: - "regular" inner classes - static member classes ------------------------------------------------------------------------------- 4) Inner Classes + INNER CLASS: a class within a class - its object will have reference (the "this" ref) to object of TLC - you can put ICs in different places + Places for ICs in TLCs: 1. At the member level - just like a variable or method - called MEMBER CLASS 2. At the statement level - just like a statement in a method - called LOCAL CLASS 3. At the expression level: - just like an expression - called ANONYMOUS CLASS ------------------------------------------------------------------------------- 5) Nested Classes + inner classes and STATIC MEMBER CLASSES + SMC: - think "class methods," "class fields," and now "class classes" - useful for accessing other static members and making inner interfaces - other inner classes can use the inner interfaces! - we're not going to do much (if anything) with SMC ------------------------------------------------------------------------------- 6) Member Classes (member lever IC) + reminder: nonstatic classes declared at member level in TLC + Structure: (Constructors have been omitted for clarity) 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) + Restrictions: - cannot have same name as TLC or package (not that you would want to!) - cannot *contain* static members, but can have static final (constants) - no interfaces as member classes - what about static member classes? See NESTED CLASSES + How do you use a member class? - every member class is associated with instance of TLC - Valid: OuterClass oref = new OuterClass(); OuterClass.InnerClass iref = oref.new InnerClass() iref.doSomething(); and new OuterClass().new InnerClass() - Not valid: InnerClass iref = new InnerClass(); iref.doSomething(); + Interal references with `this': - inside IC, the `this' refers to current instance of IC - to get to current instance of OC, save the `this' as an OC field or use `OC.this' + Inheritance: - be careful to distinguish between class and containment hierarchies! - inner classes do inherit - can use OC.super. to access member of superclass of OC ------------------------------------------------------------------------------- (6) Local Classes (statement-level IC) + local class location: - statement level declaration - within block of code, usually methods and initializers + scope: - local to block - can access all members of the TLC - can access all FINAL variables inside the block - 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 and one of these days DIS will memorize those, too! + Structure: public class OuterClass { tlc_members methodheader { statements public class InnerClass { mc_members } statements } } + Restrictions: (resembles member classes) - cannot be used outside of block - no modifiers - no static, but can have static final (constants) - no "local interfaces" - cannot have same name of TLC ------------------------------------------------------------------------------- (7) Anonymous Class (expression-level IC) + AC location and structure: - defined and created at expression level - so, has no name and no modifiers new classname ( argumentlist ) { classbody } and new interfacename () { classbody; } + adapter class: - common use for AC - adapter class defines code that another object invokes + Restrictions: (resembles local classes) - no modifiers - no static, but can have static final (constants) - no construtors, but can use initilizers for same purpose! + When to use? - class has very short body - only one instance of class needed - class used right after defined - having a name for the class is pointless - ex) anonymous arrays: new int[] {1, 2, 3} creates array "on the spot" -------------------------------------------------------------------------------