Podcast
Questions and Answers
Where is an inner class declared?
Where is an inner class declared?
Inside the curly braces of another class, but outside any method or other code block.
What modifiers can you use on an inner class?
What modifiers can you use on an inner class?
abstract or final, along with an access modifier.
What members of its enclosing class does an inner class have access to?
What members of its enclosing class does an inner class have access to?
All of them, including private members.
What do you need to instantiate an inner class?
What do you need to instantiate an inner class?
Within a class how would you instantiate an inner class called MyInner?
Within a class how would you instantiate an inner class called MyInner?
Outside of a class called MyOuter's instance methods how would you instantiate an inner class of MyOuter called MyInner?
Outside of a class called MyOuter's instance methods how would you instantiate an inner class of MyOuter called MyInner?
What does 'this' refer to when used in code within an inner class?
What does 'this' refer to when used in code within an inner class?
How can you refer to the instance of an outer class called MyOuter to which an inner class instance is tied?
How can you refer to the instance of an outer class called MyOuter to which an inner class instance is tied?
Where is a method-local inner class defined?
Where is a method-local inner class defined?
How can a method-local inner class be used?
How can a method-local inner class be used?
Where can a method-local inner class be instantiated?
Where can a method-local inner class be instantiated?
Which variables declared within the method (including parameters) can a method-local inner class use?
Which variables declared within the method (including parameters) can a method-local inner class use?
Which modifiers apply to a method-local inner class?
Which modifiers apply to a method-local inner class?
What name do anonymous inner classes have?
What name do anonymous inner classes have?
What is the type of an anonymous inner class?
What is the type of an anonymous inner class?
An anonymous inner class is always created as part of what?
An anonymous inner class is always created as part of what?
What should always follow the closing } of an anonymous inner class?
What should always follow the closing } of an anonymous inner class?
What methods can you call on an anonymous inner class reference?
What methods can you call on an anonymous inner class reference?
What are the rules for an anonymous inner class relating to extending classes and implementing interfaces?
What are the rules for an anonymous inner class relating to extending classes and implementing interfaces?
How is an argument-defined anonymous inner class declared, defined and instantiated?
How is an argument-defined anonymous inner class declared, defined and instantiated?
What should follow the closing } for an argument-defined anonymous inner class?
What should follow the closing } for an argument-defined anonymous inner class?
What are static nested classes?
What are static nested classes?
Is a static nested class actually an inner class?
Is a static nested class actually an inner class?
Do you need an instance of the outer class to instantiate a static nested class?
Do you need an instance of the outer class to instantiate a static nested class?
What does it require to instantiate a static nested class named Nested found in an outer class named BigOuter?
What does it require to instantiate a static nested class named Nested found in an outer class named BigOuter?
How can a static nested class access the non-static members of its enclosing class?
How can a static nested class access the non-static members of its enclosing class?
Study Notes
Inner Classes
- An inner class is declared inside the curly braces of another class, outside any method.
- Inner classes can use modifiers like abstract, final, and access modifiers because they are members of the enclosing class.
- Inner classes have access to all members of their enclosing class, including private members.
- Instantiation of an inner class requires a reference to an instance of the outer class.
Instantiation
- To instantiate an inner class named MyInner, use:
MyInner mi = new MyInner();
. - To instantiate an inner class from outside its enclosing class (MyOuter), both class names must be used along with a reference to the outer class:
- E.g.,
MyOuter mo = new MyOuter();
MyOuter.MyInner inner = mo.new MyInner();
.
- E.g.,
Contextual Reference
- The keyword "this" in an inner class refers to the inner class itself.
- To access the outer class instance tied to the inner class, use:
MyOuter.this
.
Method-Local Inner Classes
- A method-local inner class is defined within a method of the enclosing class.
- It must also be instantiated to be used, and can only be instantiated within the same method after its definition.
- Method-local inner classes can only use parameters and variables declared within the method if marked as final.
Anonymous Inner Classes
- Anonymous inner classes do not have a specific name and are either subclasses of a named class or implement a named interface.
- They are always created as part of a statement and must follow with a semicolon
;
after the closing bracket. - Method calls on an anonymous inner class reference can only invoke methods defined in the reference variable’s class or interface.
Extension and Implementation Rules
- An anonymous inner class can extend one class or implement one interface, but cannot do both or implement multiple interfaces.
- Argument-defined anonymous inner classes are declared as part of a method invocation, with a closing parenthesis and semicolon following the class definition.
Static Nested Classes
- Static nested classes are inner classes that are marked with the static modifier.
- They are not true inner classes; they are top-level nested classes and do not require an instance of the outer class for instantiation.
- Static nested classes access non-static members of their enclosing class only if an instance of the outer class is explicitly passed; otherwise, they cannot access them.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Test your knowledge of Java inner classes with these flashcards. Explore where inner classes can be declared, the modifiers applicable to them, and their access to the enclosing class's members.