Java - Member Inner Class

Introduction

A member inner class is declared inside a class along with member field or method.

It can be declared as public, private, protected, or package-level.

The instance of a member inner class may exist only within the instance of its enclosing class.

Example

Tire Is a Member Inner Class of the Car Class

class Car {
  // A member variable for the Car class
  private int year;

  // A member inner class named Tire
  public class Tire {
    // A member variable for the Tire class
    private double radius;

    // Constructor for the Tire class
    public Tire(double radius) {
      this.radius = radius;
    }

    // A member method for the Tire class
    public double getRadius() {
      return radius;
    }
  } // Member inner class declaration ends here

  // A constructor for the Car class
  public Car(int year) {
    this.year = year;
  }

  // A member method for the Car class
  public int getYear() {
    return year;
  }
}

Related Topics