7. Inner Classes Flashcards

1
Q

What is a key benefit of an inner class?

A
One of the key benefits of an inner class is the special relationship an inner class instance shares with
an instance of the outer class. That special relationship gives code in the inner class access to
members of the outer class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is an inner class to an outer class?

A
The inner class is a part of the outer class. An inner class instance has
access to all members of the outer class, even those marked private.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to acces the inner class?

A
A regular inner class cannot have static declarations of any kind. The only way you can access the
inner class is through a live instance of the outer class. To create an instance of an inner class, you
must have an instance of the outer class to tie to the inner class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to create an instance of the inner class?

A

MyOuter.MyInner inner = new MyOuter().new Myinner();

Or if you already have an instance of the outer class:

MyOuter.MyInner inner = outerObjRef.new MyInner();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Where does this refers to in the inner class?

A
Within the inner class code, the this reference refers to the instance of the inner class. To reference
the “outer this” from within the inner class code, use NameOfOuterClass.this (example,
MyOuter.this)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which acces modifiers can be applied to inner classes?

A
A regular inner class is a member of the outer class just as instance variables and methods are, so the
following modifiers can be applied to an inner class:
- Final
- Abstract
- Public
- Private
- Protected
- Static
- Strictfp
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a Method-local inner class?

A
You can also define an inner class within a method. To use the inner class, you must make an
instance of it somewhere within the method but below the inner class definition
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can a method local inner class be instantiated?

A
A method-local inner class can be instantiated only within the method where the inner class is
defined. So no other code running in any other method – inside or outside the class – can ever
instantiate the method local inner class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Can an inner class use the local variables of the method he is in?

A

the inner class object cannot use the local variables of the method the inner class is in, unless they are marked final or are effectively final

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which acces modifiers can be applied to method local inner classes?

A
the only modifiers you can apply to a method-local inner class are abstract and final, but never both at the
same time.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Can a method local inner class be defined in a static method?

A
Remember that a local class declared in a static method has access only to static
members of the enclosing class, since there is no associated instance of the enclosing class. If you are
in a static method, there is no this, so inner class in a static method is subject to the same restrictions
as the static method.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is an anonymous inner class?

A

Anonymous inner classes are declared without any classname at all. And if thats not weird enough,
you can define these classes, not just within a method, but even within an argument to a method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the point of making an anonymous inner class?

A

The whole point of making an anonymous inner class, is to override one or more methods of the
superclass.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Which methods can you call in an anonymous inner class?

A
You can only call methods on an anonymous inner class reference that are defined in the reference
variable type. This is no different from any other polymorphic reference.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are Anonymous interface implementers?

A
One more thing to keep in mind about anonymous interface implementers: they can implement only
one interface. They can’t even extend a class and implement an interface at the same time.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

how to end a anonymous inner class?

A
  • If they are argument local, they end like this: });

- If they are just plain old anonymous classes, then they end like this: };

17
Q

What is a static nested class?

A
static nested classes aren’t really inner classes at all based on the standard definition of an inner
class. A static nested class also doesn’t have that special relationship with the outer class. It is simply
a non-inner class scoped within another. A static nested class is simply a class that is a static member
of the enclosing class: class BigOuter{ static class Nested{ } }
18
Q

Is a static nested class really static?

A
The class itself is not really static, there is no such thing as a static class. The static modifier in this
case says that the nested class is a static member of the outer class. That means it can be accessed,
as with other static members, without having an instance of the outer class.
19
Q

Does a static nested class have acces to the instance variables?

A
Just as a static method does not have access to the instance variables and nonstatic
methods of the class, a static nested class does not have access to the instance variables and
nonstatic methods of the outer class.