Java - Creating Objects of Inner Classes


An instance of a member inner class always exists within an instance of its enclosing class.

The general syntax to create an instance of a member inner class is as follows: MemberInnerClassConstructor()

Consider the following code

class Outer {
   public class Inner {

You need to use the new operator on the out reference variable to create an object of the Inner class. Inner();

To store the reference of the instance of the Inner member inner class in a reference variable:

Outer.Inner in = Inner();


class Car {
  int year;/*  w w w. j a va  2s.  co m*/

  public Car(int y) {
    year = y;

  public class Tire {
    private double price;

    public Tire(double d) {
      price = d;

    public double getPrice() {
      return price;

  public int getYear() {
    return year;


public class Main {
  public static void main(String[] args) {
    // Create an instance of Car with year as 2015
    Car c = new Car(2015);

    // Create a Tire for that car
    Car.Tire t = Tire(9.0);

    System.out.println("Car's year:" + c.getYear());
    System.out.println("Car's tire :" + t.getPrice());