Use LEFT and RIGHT arrow keys to navigate between flashcards;
Use UP and DOWN arrow keys to flip the card;
H to show hint;
A reads text to speech;
30 Cards in this Set
- Front
- Back
Describe the "special relationship" that an inner class instance shares with an instance of the outer class.
|
It gives code in the inner class access to members of the enclosing (outer) class - even those marked private, as if the inner class were part of the outer class (or really a full-fledged member of the outer class).
|
|
When we say "regular" inner class , what is it not?
|
it is not
1. static 2. method-local 3. anonymous |
|
What is the only way you can access a regular inner class?
|
Through a live instance of the outer class
|
|
To instantiate an instance of a regular inner class, what must you have?
|
An instance of the outer class to tie to the inner class.
|
|
Why can't you instantiate a regular inner class from a "static" method of the outer class?
|
Because in static code, there is no "this" reference
|
|
If you have an outer class MyOuter with a regular inner class MyInner, what two class files do you get when you do:
%javac MyOuter.java |
MyOuter.class
MyOuter$MyInner.class Note: the inner class file isn't accessible to you in the usual way. |
|
What code would you use in a main method outside the outer class MyOuter to make an instance of the regular inner class MyInner?
|
either:
MyOuter mo = new MyOuter(); MyOuter.MyInnner inner = mo.new MyInner(); or: MyOuter.MyInner inner = new MyOuter().new MyInner(); |
|
How does an object refer to itself normally?
|
Using the "this" reference.
|
|
To reference the regular inner class instance itself, from within the inner class code, use _____.
|
this
|
|
To reference the "outer this" (the outer class instance) from within the regular inner class code, use _____.
|
.this (example, MyOuter.this)
|
|
A regular inner class is a member of the outer class so what modifiers can be applied to it?
|
final, abstract, public, private, protected, static (except this turns it into a static nested class rather than a regular inner class), strictfp
|
|
A regular inner class is scoped where?
|
inside another class' curly braces, but outside any method code.
|
|
What is a method-local inner class?
|
An inner class defined within a method.
|
|
What is the only place that a method-local inner class can be instantiated?
|
Only within the method where the inner class is defined and it must be below the inner class definition.
|
|
Can a method-local inner class use the local variables of the method the inner class is in? Why or Why not?
|
No, unless the local variables are marked final. Because local variables live on the stack so aren't guaranteed to be alive as long as the method-local inner class object which lives on the heap.
|
|
What happens if you try to access a local variable within a method-local inner class?
|
The compiler gets upset - it won't compile (unless it is final).
|
|
What are the only modifiers you can apply to a method-local inner class?
|
abstract and final (but, of course, never both at the same time).
|
|
If you're in a static method, there is no ____, so an inner class in a static method is subject to the same restrictions as the static method. In other words, no access to _____.
|
"this"
"instance variables" |
|
A local calls declared in a static method has access to only ____ members of the enclosing class, since there is no associated _____ of the enclosing class.
|
"static"
"instance" |
|
What is the whole point of making an anonymous inner class?
|
To override one or more methods of the superclass or to implement methods of an interface.
|
|
What is the syntax for closing off an anonymous inner class definition?
|
");" Notice especially the semicolon.
|
|
Can you call a method in an anonymous inner class that is a new method (i.e. does not override a method in the superclass)?
|
No - you get a compiler error.
|
|
What are the two flavors of anonymous inner classes?
|
1. creates an anonymous subclass of the specified class type.
2. creates an anonymous implementer of the specified interface type. |
|
How many interfaces can an anonymous inner class implement?
|
one
|
|
If you have an anonymous inner class defined right inside the argument, what is the syntax at the end of it?
|
"});" end inner class def, arg, and end statement
|
|
Does an instance of an static nested class enjoy a special relationship with an instance of the outer class?
|
No
|
|
What is a staic nested class?
|
A class that's a static member of the enclosing class. It does not have access to the instance variables and methods of the outer class.
Note: static means it can be accessed without having an instance of the outer class. |
|
Give the syntax for instantiating a static nested class Nested with the outer class named BigOuter.
|
BigOuter.Nested n = new BigOuter.Nested();
|
|
Do you need an instance of the enclosing class to instantiate a static nested inner class?
|
No - they do not share a special relationship with any instance of the enclosing class
|
|
Why can't a static nested class access nonstatic members of the outer class?
|
Because it does not have an implicit reference to any outer instance (it does not get an outer "this" reference).
|