Java - Nested Classes

java

// Nested class
class OuterClass {
    ...
    class NestedClass {
        ...
    }
}
// A nested class is a member of its enclosing class. 
// Non-static inner classes have access to other members of the enclosing class, even if they are declared private. 
// Static nested classes do not have access to other members of the enclosing class. 
// A nested class can be declared private, public, protected, or package private. 
// (Outer classes can only be declared public or package private.)

// Nested class is a logical way to group classes that are only used in one place. If a class is useful to 
// only one other class, then it is logical to embed it in that class and keep the two together. Nesting 
// such "helper classes" makes their package more streamlined

// Using nested classes increases encapsulation. Consider two top-level classes, A and B, where 
// B needs access to members of A that would otherwise be declared private. By hiding class B within 
// class A, A's members can be declared private and B can access them. In addition, B itself can be 
// hidden from the outside world.

// Using nested classes can also lead to more readable and maintainable code: Nesting small classes 
// within top-level classes places the code closer to where it is used.

// As with class methods and variables, a static nested class is associated with its outer class. And like 
// static class methods, a static nested class cannot refer directly to instance variables or methods defined 
// in its enclosing class: it can use them only through an object reference.

// A static nested class interacts with the instance members of its outer class (and other classes) just 
// like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has 
// been nested in another top-level class for packaging convenience.

// Access static inner class
OuterClass.StaticNestedClass

// To create an object for the static nested class, use this syntax:
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();

// As with instance methods and variables, an inner class is associated with an instance of its enclosing 
// class and has direct access to that object's methods and fields. Also, because an inner class is associated 
// with an instance, it cannot define any static members itself.  Objects that are instances of an inner class 
// exist within an instance of the outer class.

// 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.InnerClass innerObject = outerObject.new InnerClass();

// if the inner class has a member with the same name as a member of the outer class, a method of an 
// inner class access a member of the outer class:
OuterClassName.this.memberName

// An anonymous inner class is an inner class (a class defined inside another class) that is defined like this:
new ItemListener() { ... }

// The class defined by { ... } does not have a name.  It extends ItemListener, and is instantiated right on 
// the same line (not necessarily as soon as the compiler see the class definition).

// Another example of anonymous class:
private static String[] reverseSort(String[] array) {
  Comparator<String> reverseComparator = new Comparator<String>() {
    public int compare(String string1, String string2) {
      return string2.compareTo(string1);
    }
  };
  Arrays.sort(array, reverseComparator);
  return array;
}

// In the above code, we implement an anonymous class that extends the Comparator class, create an 
// instance of this anonymous class, assign it to reverseComparator, and we pass this instance as the second
// parameter to the Arrays.sort function.

// In general, we should avoid using anonymous class because it is only usable where you define it.  It cannot 
// be re-used.  Anonymous classes should only be used in a limited case where only implement small amount 
// of logic.  If we need to put a lot of logic into an anonymous class, we should make it into a full class with 
// a name.

Can we nest one class inside another class?

Yes.

class OuterClass {
    ...
    class NestedClass {
        ...
    }
}

A nested class is a member of its enclosing class. Non-static inner classes have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class. A nested class can be declared private, public, protected, or package private. (Outer classes can only be declared public or package private.)

Can an inner class have access to the outer class?

Yes, even if the member of the outer class was declared as private. Non-static inner classes have access to other members of the enclosing class, even if they are declared private.

What are 4 reasons that we should use nested classes?

  1. It is a way of logically grouping classes that are only used in one place: If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.
  2. It increases encapsulation: Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.
  3. It can lead to more readable and maintainable code: Nesting small classes within top-level classes places the code closer to where it is used.

What is the purpose of static nested class?

As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference.

A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.

How can we access static inner class?

OuterClass.StaticNestedClass

// To create an object for the static nested class, use this syntax:
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();

Does an inner class have access to the outer class?

As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself. 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.InnerClass innerObject = outerObject.new InnerClass();

Can we create an instance of an inner class without creating an instance of the outer class?

No. 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.InnerClass innerObject = outerObject.new InnerClass();

How can a method of an inner class access a member of the outer class if the inner class has a member with the same name?

We can use OuterClassName.this.memberName:

public class ShadowTest {

    public int x = 0;

    class FirstLevel {

        public int x = 1;

        void methodInFirstLevel(int x) {
            System.out.println("x = " + x);
            System.out.println("this.x = " + this.x);
            System.out.println("ShadowTest.this.x = " + ShadowTest.this.x);
        }
    }

    public static void main(String... args) {
        ShadowTest st = new ShadowTest();
        ShadowTest.FirstLevel fl = st.new FirstLevel();
        fl.methodInFirstLevel(23);
    }
}

In the above code, both ShadowTest and its inner class each have a property name x, and inside the methodInFirstLevel method, to access x that is part of FirstLevel, we use this.x, and to access x that is in ShadowTest, we use ShadowTest.this.x

Is serialization of inner class encouraged?

No. Serialization of inner classes, including local and anonymous classes, is strongly discouraged. When the Java compiler compiles certain constructs, such as inner classes, it creates synthetic constructs; these are classes, methods, fields, and other constructs that do not have a corresponding construct in the source code. Synthetic constructs enable Java compilers to implement new Java language features without changes to the JVM. However, synthetic constructs can vary among different Java compiler implementations, which means that .class files can vary among different implementations as well. Consequently, you may have compatibility issues if you serialize an inner class and then deserialize it with a different JRE implementation. See the section Implicit and Synthetic Parameters in the section Obtaining Names of Method Parameters for more information about the synthetic constructs generated when an inner class is compiled. https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

What is an inner class?

When class B is defined inside class A, class B inherits everything from class A. If we put a class inside another class, the inner class can see all the variables and methods in the outer class.

What is an anonymous inner class?

An anonymous inner class is an inner class (a class defined inside another class) that is defined like this:

new ItemListener() { ... }

The class defined by { … } does not have a name. It extends ItemListener, and is instantiated right on the same line (not necessarily as soon as the compiler see the class definition).

How can we implement an anonymous class?

private static String[] reverseSort(String[] array) {
  Comparator<String> reverseComparator = new Comparator<String>() {
    public int compare(String string1, String string2) {
      return string2.compareTo(string1);
    }
  };
  Arrays.sort(array, reverseComparator);
  return array;
}

In the above code, we implement an anonymous class that extends the Comparator class, create an instance of this anonymous class, assign it to reverseComparator, and we pass this instance as the second parameter to the Arrays.sort function.

Why should we avoid using anonymous class?

In general, we should avoid using anonymous class because it is only usable where you define it. It cannot be re-used. Anonymous classes should only be used in a limited case where only implement small amount of logic. If we need to put a lot of logic into an anonymous class, we should make it into a full class with a name.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License