开发者

Regarding an example illustrating the usage of generic class syntax

In thinking in Java, page 566 gives the following example.

class CountedInteger {
  private static long counter;
  private final long id = counter++;
  public String toString() { return Long.toString(id); }
}
public class FilledList<T> {
  private Class<T> type;

  public FilledList(Class<T> type) { this.type = type; }

  public List<T> create(int nElements) {
    List<T> result = new ArrayList<T>();
    try {
       for(int i = 0; i < nElements; i++)
       result.add(type.newInstance());
    } catch(Exception e) {
      throw new RuntimeException(e);
    }
    return result;
  }

   public stat开发者_运维百科ic void main(String[] args) {
     FilledList<CountedInteger> fl = new FilledList<CountedInteger>(CountedInteger.class);
     System.out.println(fl.create(15));
   }
} 

I have three questions with respect to this example.

1) What is the usage of private Class type? Why is it private?

2) Why do the following, in particular " this.type = type;"

public FilledList(Class<T> type) { this.type = type; }

3) The author claims:

Notice that this class must assume that any type that it works with has a default constructor (one without arguments), and you’ll get an exception if that isn’t the case.

I can not figure out how this statement was reflected in the above example. Thanks.


1) It doesn't have to be, but that's one of the things you do in Java--don't expose things that don't need to be exposed.

2) That sets the property to the parameter of the constructor--that's elemental Java.

3) Because of the type.newInstance() call; without a default (no-arg) constructor it will fail.


  1. It doesn't have to be private, but generally it is good to hide internal fields in your classes, unless you want them to be accessible from the outside. I would also put final modifier on type filed to prevent modification.

  2. This line assigns value passed in the class constructor to the instance field named type. Keyword this indicates that it is a this instance field.

  3. The type.newInstance() code will fail if corresponding type doesn't have a default constructor.

BTW, the whole thing can be replaced with java.util.Collections.fill(list, value) call.


  1. The type is just another instance variable. It is declared private as to prevent access to it from outside of this class.

  2. When an instance of this class FilledList is created, ie, when the constructor which takes 1 arg is called, the instance variable type is initilized with this arg value passed in the constuctor.

  3. What type.newInstance() does is call the default no args constructor of this type, this would throw an exception if the default constructor is private.


1: all members of a class should be private or protected with respect to information hiding. But you dont have to do that

2: Inside any method ou have access to all parameters and all members-variables in that class. In this case the parameter-name is exactly the same as a member-variable. So in this case the parameter-name will win if you write type. this is pointing to the current instance and with this.type you indicate that you mean the member-variable and not the parameter. So this.type = type is in this case member = parameter.

3: newInstance with no parameters will call a default-constructor on T and if it doesn't exist an exception will be thrown. take a look at the link

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜