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?
Signup and view all the answers
Within a class how would you instantiate an inner class called MyInner?
Within a class how would you instantiate an inner class called MyInner?
Signup and view all the answers
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?
Signup and view all the answers
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?
Signup and view all the answers
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?
Signup and view all the answers
Where is a method-local inner class defined?
Where is a method-local inner class defined?
Signup and view all the answers
How can a method-local inner class be used?
How can a method-local inner class be used?
Signup and view all the answers
Where can a method-local inner class be instantiated?
Where can a method-local inner class be instantiated?
Signup and view all the answers
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?
Signup and view all the answers
Which modifiers apply to a method-local inner class?
Which modifiers apply to a method-local inner class?
Signup and view all the answers
What name do anonymous inner classes have?
What name do anonymous inner classes have?
Signup and view all the answers
What is the type of an anonymous inner class?
What is the type of an anonymous inner class?
Signup and view all the answers
An anonymous inner class is always created as part of what?
An anonymous inner class is always created as part of what?
Signup and view all the answers
What should always follow the closing } of an anonymous inner class?
What should always follow the closing } of an anonymous inner class?
Signup and view all the answers
What methods can you call on an anonymous inner class reference?
What methods can you call on an anonymous inner class reference?
Signup and view all the answers
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?
Signup and view all the answers
How is an argument-defined anonymous inner class declared, defined and instantiated?
How is an argument-defined anonymous inner class declared, defined and instantiated?
Signup and view all the answers
What should follow the closing } for an argument-defined anonymous inner class?
What should follow the closing } for an argument-defined anonymous inner class?
Signup and view all the answers
What are static nested classes?
What are static nested classes?
Signup and view all the answers
Is a static nested class actually an inner class?
Is a static nested class actually an inner class?
Signup and view all the answers
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?
Signup and view all the answers
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?
Signup and view all the answers
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?
Signup and view all the answers
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.