Study your flashcards anywhere!

Download the official Cram app for free >

  • Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

How to study your flashcards.

Right/Left arrow keys: Navigate between flashcards.right arrow keyleft arrow key

Up/Down arrow keys: Flip the card between the front and back.down keyup key

H key: Show hint (3rd side).h key

A key: Read text to speech.a key

image

Play button

image

Play button

image

Progress

1/13

Click to flip

13 Cards in this Set

  • Front
  • Back
What are the 4 kinds of inner classes?
"regular"
static
method-local
anonymous
class MyOuter{
class MyInner{}
}

What class files are created?
MyOuter.class
MyOuter$MyInner.class
%java MyOuter$MyInner

What happens when you try try to run this regular inner class?
Can't run because regular inner classes can't have static methods (like main()).

The ONLY way you can access a regular inner class is through an instantiated object of the outer class which owns it.
How do you write a declaration for an inner class in an outside class? How do you instantiate and instance of the inner class in this outside class?
MyOuter.MyInner moi = new Myouter().new MyInner();

NOTE: You don't have a direct reference to the outer object in this example.
How do you declare and instantiate a regular inner class object from within an outer object (i.e. inside MyOuter object)
class MyOuter{
MyInner mi = new MyInner();
class MyInner{}
}
"this" is a keyword an object uses to refer to itself. How does an inner class object get at it's outer object's "this"?
MyOuter.this
How can a regular inner class be modified?
Think of regular inner class as just another class member.

It is modified in manner that is close to how member variables and member methods are modified.

static (this then wouldn't be a regular inner class - becomes a nested class, not really an inner class)

final
abstract
strictfp

public
protected
default
private

NOTE: Regularly classes can't be marked private or protected.
When creating an instance of a method local inner class, what conditions must be true?
Method local inner classes have many similarities to any other local variable inside a method.

1) Obviously the inner class itself is defined inside the method.

2) Any instantiation of this method local inner class must occur in the same method as where the method local inner class definition occurs.

3) The instantiation must come AFTER the class definition. You CANNOT have a line of code that instantiates a method local inner class and then define the inner class itself later in the method.
Can a method local inner class reference other local variablesin that same member? Why or why not?
Only when the other local variables are marked final. Same is true for any method arguments.

Local variables live on the stack and when the method ends they need to be able to go away. Method local inner class objects live on the heap and might continue to exist after the method has been returned from. The mustn't have references to stack variables that no longer exist.

Marking local variables final allows the compiler to optimize and include such local variables in the object.
What modifiers can you use when declaring a method local inner class?
final
abstract

Method local inner classes are similar to local variables which can only be marked final.
Why do you put a semicolon after the closing bracket of an anonymous inner class?
You don't necessarily. Annonyous inner classes are used inside statements. After declaring an anonymous inner class you may still have to end the statement, which might include a semicolon or other characters.

I.E.
Object obj = new Object(){
...stuff...
};
How can you use the new keyword to create an instance of an interface?
Runnable r = new Runnable(){
public void run(){}
};

You can't actually instantiate an interface, but you can instantiate an object that implements the interface. In the example above you're creating an Object that implements the interface.

NOTE: This is NOT allowed:
Runnable r = new Runnable();
How many interfaces can an anonymous inner class implement?
Many.

If an anonymous inner class descends from a class that implmements many interfaces, it too implements those interfaces.

If an anonymous inner class uses and interface after the new keyword, then it descends from Object and implements just that 1 interface.

NOTE: There is no use of the keywords "extends" or "implements" with annonymous inner classes.