โ† All SCJP Flashcard Decks

Inner Classes and Nested Types Flashcards

6 cards from real SCJP practice questions. Tap to flip, then mark Knew It or Still Learning โ€” missed cards come back until you master them.

Read the first 6 Inner Classes and Nested Types flashcards as text
  1. Which statement about a static nested class is TRUE?

    Answer: It can be instantiated without an instance of the outer class

    A static nested class is associated with the outer class itself, not with an instance. You instantiate it as `new Outer.Nested()` without needing an Outer instance. It cannot access instance members of the outer class directly.

  2. What is the correct syntax to create an instance of the non-static inner class `Inner` defined inside class `Outer`?

    Answer: Outer o = new Outer(); Outer.Inner i = o.new Inner();

    A non-static inner class is tied to an instance of its enclosing class. You must first create an Outer instance, then use the `outerRef.new Inner()` syntax. The other forms are not valid Java.

  3. A method-local inner class defined inside an instance method can access which variables from the enclosing method?

    Answer: Only local variables declared final (or effectively final in Java 8+)

    A method-local inner class can access the enclosing class's instance/static members freely, but can only access local variables that are final (Java 6 SCJP rule). This is because the class instance may outlive the method's stack frame.

  4. Which of the following is a valid way to create an anonymous class in Java?

    Answer: Runnable r = new Runnable() { public void run() {} };

    Anonymous classes are created with `new InterfaceOrClass() { /* body */ }`. There is no `anonymous` keyword, no `class` keyword in the syntax, and no parentheses-only form without `new`.

  5. Can a non-static inner class declare static members?

    Answer: Yes, but only static final constant fields

    In Java, a non-static inner class may declare static final constant fields (compile-time constants) but cannot declare other static members. This is because the inner class is associated with an outer instance and full static context would be ambiguous.

  6. What is the correct way for an inner class to reference the enclosing class's instance variable `x` when the inner class has its own variable `x`?

    Answer: Outer.this.x

    `Outer.this` is the qualified `this` reference that refers to the enclosing outer class instance. `super.x` refers to a superclass field, `outer.x` requires an explicit reference variable named `outer`, and `this.outer.x` is not valid Java syntax.

How this page was made: written with AI tools and published by Nathan Beja, founder of Practice Test Geeks, who is responsible for its content. We have not yet matched this exam's facts to an official source, so confirm fees, dates and rules with the exam body. Not reviewed by a subject-matter expert. Editorial policy

Inner Classes and Nested Types Flashcards โ€” SCJP Study Cards with Answers