简体   繁体   中英

Inner class and two constructors

I am new to java.I came across this code somewhere on the internet.

  class FactoryOuter {

  FactoryInner[] fi = new FactoryInner[3];
  private int lastIndex = 0;
  private int x = 0;

  public FactoryOuter(int x)
  {
    this.x = x;
  }

  public int getX()
  {
    return x;
  }

  public void addInner(int y)
  {
    if (lastIndex < fi.length)
    {
      fi[lastIndex++] = new FactoryInner(y);
    }
    else throw new RuntimeException("FactoryInner array full");
  }

  public void list()
  {
    for (int i = 0; i < fi.length; i++)
    {
      System.out.print("I can see into the inner class where y = " +
                       fi[i].y + " or call display: ");
      fi[i].display();
    }
  }

  public class FactoryInner
  {
    private int y;
    private FactoryInner(int y)
    {
      this.y = y;
    }
    public void display()
    {
      System.out.println("FactoryInner x =  " + x + " and y = " + y);
    }
  }
}

public class FactoryInnerOuter
{

    public static void main(String[] args)
    {
        FactoryOuter fo = new FactoryOuter(1);
        fo.addInner(101);
        fo.addInner(102);
        fo.addInner(103);
        fo.list();
        //fo.addInner(104);
    }
}

The output of the above code is given below

I can see into the inner class where y = 101 or call display: FactoryInner x =  1 and y = 101
I can see into the inner class where y = 102 or call display: FactoryInner x =  1 and y = 102
I can see into the inner class where y = 103 or call display: FactoryInner x =  1 and y = 103

My questions are as follows

1) I see the constructor of FactoryInner class being called twice.The first instance apparently calls a default constructor as shown below.

 FactoryInner[] fi = new FactoryInner[3];

The second instance calls the constructor that is defined in the code.

fi[lastIndex++] = new FactoryInner(y);

Why does the code need two constructors? If I replace

FactoryInner[] fi = new FactoryInner[3]; with

FactoryInner[] fi;

I do get the following errors.

Exception in thread "main" java.lang.NullPointerException
    at FactoryOuter.addInner(FactoryInnerOuter.java:19)
    at FactoryInnerOuter.main(FactoryInnerOuter.java:56)

Why are two construcors needed ? Is this a particulary bad example? Could someone please explain.

This

fi[lastIndex++] = new FactoryInner(y);

Initializes the FactoryInner reference at index lastIndex in the FactoryInner array fi .

This

FactoryInner[] fi = new FactoryInner[3]; 

initializes the fi FactoryInner array. Notice the square brackets, [] .

You have to initialize the array first before you can initialize its elements. Otherwise you try to access a member of a null array reference and get a NullPointerException .

The first is not a call to the constructer. Here you are initializing an array of FactoryInner objects with room for three objects:

FactoryInner[] fi = new FactoryInner[3]; 

Here you are putting actual FactoryInner objects into the array, hence you need to give a value to the constructor because now you are actually initializing objects from the class FactoryInner:

fi[lastIndex++] = new FactoryInner(y);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM