Chapter 8: Inner Classes Flashcards

1
Q
What is the result of compiling the following class?
class MyOuter {
   class MyInner { }
}
A
Two class files:
MyOuter.class
MyOuter$MyInner.class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
True or False: 
An inner class instance can never stand alone without a direct relationship to an instance of the outer class.
A

True

Static inner classes can ofcourse. The only way a regular inner class can be created is through its outer class.

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

What is the result?

class MyOuter {
   public static void main(String[] args) {
      MyOuter.MyInner myInner = new MyOuter.MyInner();
      myInner.doStuff();
   }
   class MyInner { 
      void doStuff() { System.out.println("test"); }
   }
}
A

Compiler error.

You can only create an instance of the inner class using an instance of the outer class. For example:
MyOuter myOuter = new MyOuter();
MyOuter.MyInner myInner = myOuter.new MyInner();

or

MyOuter.MyInner myInner = new MyOuter().new MyInner();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
Given the following class:
class MyOuter {
   class MyInner { 
      void doStuff() {
      // reference to MyOuter?
      }
   }
}

How can you reference to MyOuter?

A

MyOuter.this

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

What are valid modifiers for a (regular) inner class?

A
final
abstract
public
private
protected
strictfp

(static but then it turns into a static nested class)

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

What are the types of inner classes?

A

Inner classes
Method-local inner classes
Anonymous inner classes
Static nested classes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
What is the result?
class MyOuter {
      void doStuff() {
         new MyInner();
         class MyInner { 
         }
      }
}
A

Compiler error!

Method-local inner classes should first be declerated before they can be instansiated.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
What is the result?
class MyOuter {
     private int y = 10;
      void doStuff() {
         int x = 5;
         new MyInner().doInnerStuff();
         class MyInner { 
            void doInnerStuff() {
               System.out.println(x + y);
            }
         }
      }
}
A

Compiler error!

Method-local inner classes can access the outer class instance variables like a normal inner class (y). But can’t access the method variable (x). The reason is that the instance of the method-local inner class and y both live on the heap, whereas x lives on the stack.

To make this work, you must mark x final.

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

What are valid modifiers for a method-local inner class?

A

final or abstract.

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

What is wrong with the anonymous class declaration?

Popcorn p = new Popcorn() {
   public void pop() {
      System.out.println("pop");
   }
}
A

Missing ; (semicolon) an the last line.

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

True or False:

You cannot create anonymous inner classes from interfaces.

A

False

You can.

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

How to create an instance of the static nest class from outside the BigOuter class?

class BigOuter {
   static class Nest {}
}
A

new BigOuter.Nest();

From inside the BigOuter class: new Nest();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
Given:
class A
{
}
public class TestClass
{
	public class A
	{
		public void m() { }
	}
	class B extends A
	{
	}
	public static void main(String args[])
	{
		new TestClass().new A() { 	 public void m() { } };
	}
}

Select the correct options:
A Class created inside the main method is static.
B Class created inside the main method is final.
C Objects of class B cannot be created inside the main method just by doing “new B()”

A

B - Because it is anonymous. Anonymous classes are implicitly final.

C - you’ll need to do: new TestClass().new B() because inner class B is not static.

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

Which of the following statements regarding inner classes are true ?

A. A non static inner class may have static members.
B. Anonymous inner classes cannot have any 'extends' or 'implements' clause.
C. Anonymous inner classes can only be created for interfaces.
D. Anonymous inner classes can never have initialization parameters.
E. Anonymous inner classes cannot be static.
A

A. If you make them final.
B.
E.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
Which of the following statements are true?
A. An inner class may be declared static
B. Anonymous inner class may be declared public.
C. Anonymous inner class may be declared private.
D. Anonymous inner class may be declared protected.
E. Anonymous inner class may extend an abstract class.
A

A, E

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