Chapter 1. Lambda Expressions

1.1.  Describe Java inner classes and develop the code that uses Java inner classes (such as: nested class, static class, local class and anonymous classes)

Question 010101

Given the class:

public class Building {

    class Apartment {
        public int getLivingSpace() {
            return 990; // square feet
        }
    }

    public static void main(String[] args) {
        // line 1
        System.out.println("Living space : " + space);
    }
}
					

Which line inserted at // line 1 position will make the code print apartment's living space?

Options (select 1):

  1. int space = new Building().new Apartment().getLivingSpace();

  2. int space = new Building().Apartment().getLivingSpace();

  3. int space = new Building.Apartment().getLivingSpace();

  4. int space = new Apartment().getLivingSpace();

Answer:

The correct option is 1.

Objects that are instances of an inner class exist within an instance of the outer class. Consider the following classes:

class OuterClass {
    ...
    class InnerClass {
        ...
    }
}
					

An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance.

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:

OuterClass outerObject = new OuterClass();
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
					

or

OuterClass.InnerClass innerObject = new OuterClass().new InnerClass();
					

Option 2 is invalid - has wrong syntax.

Option 3 is invalid - it shows nested static class syntax.

Option 4 is invalid - inner class instance can not exist without outer class instance.

Source:

The Java Tutorials - Nested Classes [http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html]

Question 010102

Given the Room class:

public class Room {

    private String EXIT = "an exit"; // line 1

    static class Window {
        public void useWindow() {
            System.out.printf("I'm %s!", EXIT);
        }
    }

    public static void main(String[] args) {
        ... // assume 'window' variable declaration and initialization syntax correct
        window.useWindow();
    }
}
					

What is the result?

Options (select 1):

  1. The code prints "I'm an exit!"

  2. The code fails to compile. In order to compile, change the line 1 as follows:

    public String EXIT = "an exit";

  3. The code fails to compile. In order to compile, change the line 1 as follows:

    final String EXIT = "an exit";

  4. The code fails to compile. In order to compile, change the line 1 as follows:

    static String EXIT = "an exit";

Answer:

The correct option is 4.

The Window is a static nested class, as it's declared as a static member of another class.

A static nested class DOES NOT have a reference to a nesting instance, so a static nested class CANNOT invoke non-static methods or access non-static fields of an instance of the class within which it is nested.

The code will not compile, to fix the code you need to change line 1 to:

static String EXIT = "an exit";
					

Option 1 is invalid - the code will not compile. With change EXIT to static it would print I'm an exit!

Option 2 is invalid. A static nested class can access static data members of outer class including private.

Option 3 is invalid - the code does not compile due to other reason.

Source:

The Java Tutorials - Nested Classes [http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html]

Question 010103

Given the Bag class:

public class Bag {
    public void packFood(String bagName) { // line 1
        class Food { // line 2
            private void pack() { // line 3
                System.out.println("Packing into " + bagName);
            }
        }
        Food f = new Food();
        f.pack();
    }

    public static void main(String[] args) {
        new Bag().packFood("paper bag");
    }
}
					

What is the result?

Options (select 1):

  1. Code compilation fails. To make the code compile, change line 1 as follows:

    public void packFood(final String bagName) {
    								

  2. Code compilation fails. To make the code compile, change line 2 as follows:

    static class Food {
    								

  3. Code compilation fails. To make the code compile, change line 3 as follows:

    public void pack() {
    								

  4. The code successfully runs and prints Packing into paper bag.

Answer:

The correct option is 4.

This question is testing access of method parameter from local inner class, and starting in Java SE 8, a local class can access local variables and parameters of the enclosing block that are final or effectively final. This is an example of effectively final method parameter.

A variable or parameter whose value is never changed after it is initialized is effectively final. So, compilation will not fail.

[Warning]

In Java SE 7 the correct option would be Option 1.

Option 1 is wrong. It would be correct in Java SE 7, where final would be mandatory for compiler.

Option 2 is wrong. Local class can not be static.

Option 3 is wrong. Outer class can access any private members of local class.

Source:

The Java Tutorials - Local Classes [http://docs.oracle.com/javase/tutorial/java/javaOO/localclasses.html]

Professional hosting         Free 'Oracle Certified Expert Web Services Developer 6' Guide     Exam 1Z0-810: Upgrade to Java SE 8 Programmer Study Guide