| Inner classes are classes defined at a scope smaller than a package. | |
Inner classes can be define within:
|
|
| They behave just as other classes. Extend other classes and implement interfaces. | |
Types of Inner classes:
|
|
| Naming of Inner Classes | The naming convention of and inner class is as follows:
package.EnclosingClass.InnerClass.class |
| When the class outer class is compiled, the following class is generated: | |
| EnclosingClass$StaticInnerClass.class | |
| Exam Tip | The inner class cannot have the same name as the enclosing class |
| Static Inner classes | Static inner classes (nested) are defined within other classes as static classes. |
| package mypackage;
public class MyClass { private static int staticvar; public int instancevar; public static class StaticInnerClass { } } |
|
| Exam Tip | Static inner classes can only refer to the static members of the outer class. |
| Exam Tip | It can access private static members of the outer class |
| Exam Tip | Static can classes can access static members declared: public, private, protected and default visibility. |
| Exam Tip | The data members of the static inner class can only be static.
Interface and outer classes members must be static. |
| Exam Tip | Static inner do not need instances of the outer class to create an instance of the inner class.
new MyClass.StaticInnerClass(); |
| Member Inner classes | Member inner classes are defined within other classes without the static keyword. |
| package mypackage;
public class MyClass { private static int staticvar; public int instancevar; public class MemberInnerClass { } } |
|
| Exam Tip | Member inner classes can refer to the instance and static members of the outer class. |
| Exam Tip | It can access private members of the outer class |
| Exam Tip | Member can classes can access members declared: public, private, protected and default visibility. |
| Exam Tip | The data members of the inner class cannot be static.
Interface and outer classes members can be static. |
| Exam Tip | An instance of the enclosing class is required to use the inner class. |
| Syntax to use to create the inner class: | |
| public class MyClass {
public int instancevar; public class MemberClass { } public MemberClass createMember() { return this.new MemberClass(); }
} |
|
| this.new
The this operator is required to create an instance of the member class. This is the default outer class.
this.new MemberClass() can simply be used without the this. |
|
| The name of the instant variable of the outer class can be used instead of this. | |
| EnclosingClass ec = new EnclosingClass();
EnclosingClass.MemberClass mc = ec.new nclosingClass.MemberClass(); |
|
| Or as a 1 liner. | |
| EnclosingClass.MemberClass mc = (new EnclosingClass).new EnclosingClass.MemberClass(); | |
| this scope | The meaning of the this keyword is modified for inner classes. |
| The meaning of this within and inner class is the inner class itself. | |
| To get to the this for the outer class within the inner class, prefix this with the name of the outer class. | |
| public class MyClass {
public class MemberClass { public void MemberClassMember() { //this – refers to the inner class //MyClass.this refers to the outer class //super – refers to the super class of the inner class //MyClass.super refers to the super class of the outer class } } } |
|
| Local Inner Classes | Local inner classes are defined within the scope of the enclosing class method. |
| Exam Tip | Local inner classes can access the outer class members declared: public, private, protected and default visibility as long as they are declared final. |
| Exam Tip | Local inner classes, local variables cannot be declared: public, protected, private or static. No modifiers allowed. |
| Exam Tip | Local inner classes cannot have static members. |
| Exam Tip | Local inner classes method parameter must be final if accessing the outer class arguments. |
| Exam Tip | If the method is used by the local class itself. Parameters do not have to be be final. |
| Since local inner classes can only use final variables, read only vars.
How can we change the data of the outer class? |
|
| Fooling the local inner class | Passing the local inner class as argument an array declared final. The array is final, but the array data elements can be changed and returned to the outer class data member. |
| Anonymous Classes | Anonymous classes are local inner classes that don’t have a class name. |
| It’s used when a class is desired but the name is not necessary. | |
| The keyword class, extend, implement is missing and has no modifiers. | |
| Anonymous Classes are helper classes. | |
| Right after the new someclass() syntax we write a curly bracket and write our methods. | |
| new ActionListener() {
public void actionPerformed(ActionEvent e) { …… } } |
|
| Exam Tip | An anonymous class has no constructor. |
| Exam Tip | Modifiers can not be used in anonymous classes. |
| Exam Tip | They implicitly extend a super class or implement some interface even though the extends or implements keyword is used |
| Name Convention of Anonymous classes | The following naming convention is used for the generated classes:
EnclosingClass$1.class InnerSuperClass.class EnclosingClass.class
|
| Exam Tip | Sequential numbers are assigned for each anonymous class and appended to the enclosing class. |
| Exam Tip | Even if the anonymous class is created inside other inner classes. The anonymous class names are always given the enclosing class name followed by a $ sequence number. |
| Anonymous class are most commonly used in user interface adapters to perform event handling. | |
| public class DemoFrame extends Frame {
public DemoFrame() { } private void addButton( final String text) { Button b = new Button(text); b.addActionListener( //Anonymous class is defined here new ActionListener() { public void actionPerformed(ActionEvent e) { …… } } }; } } |
|
| Instance Initializer | public class OuterClass() {
public int i; {//Instance initializer i = 5; } OuterClass() { } } |
| Instance initializer is a block of code that automatically runs every time a new instance of an object. | |
| In affect it is an anonymous class constructor. | |
Instance initializer are really limited:
|
|
| Exam Tip | Instance initializer are called before the class constructor. |