Chapter 8: Inner Classes Flashcards
What is the result of compiling the following class? class MyOuter { class MyInner { } }
Two class files: MyOuter.class MyOuter$MyInner.class
True or False: An inner class instance can never stand alone without a direct relationship to an instance of the outer class.
True
Static inner classes can ofcourse. The only way a regular inner class can be created is through its outer class.
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"); } } }
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();
Given the following class: class MyOuter { class MyInner { void doStuff() { // reference to MyOuter? } } }
How can you reference to MyOuter?
MyOuter.this
What are valid modifiers for a (regular) inner class?
final abstract public private protected strictfp
(static but then it turns into a static nested class)
What are the types of inner classes?
Inner classes
Method-local inner classes
Anonymous inner classes
Static nested classes
What is the result? class MyOuter { void doStuff() { new MyInner(); class MyInner { } } }
Compiler error!
Method-local inner classes should first be declerated before they can be instansiated.
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); } } } }
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.
What are valid modifiers for a method-local inner class?
final or abstract.
What is wrong with the anonymous class declaration?
Popcorn p = new Popcorn() { public void pop() { System.out.println("pop"); } }
Missing ; (semicolon) an the last line.
True or False:
You cannot create anonymous inner classes from interfaces.
False
You can.
How to create an instance of the static nest class from outside the BigOuter class?
class BigOuter { static class Nest {} }
new BigOuter.Nest();
From inside the BigOuter class: new Nest();
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()”
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.
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. If you make them final.
B.
E.
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, E