开发者

Generic static factory method problem

开发者 https://www.devze.com 2023-01-22 06:43 出处:网络
I have class like following: public class Tree<T extends Comparable<? super T>> { // private data omitted

I have class like following:

public class Tree<T extends Comparable<? super T>> {
  // private data omitted
  private Tree() {} // non parametric private constructor, not exposed
  // static factory method
  public static <T extends Comparable<? super T>> Tree<T> newInstance() { 
    return new Tree<T>(); 
  }
}

Now from another class I try this:

public class TestClass {
  public void test() {
    Tree<Integer> tree = Tree.newInstance(); // fail
  }
}

but when I use public constructor, following code works just fine

public void test() {
  Tree<Integer> tree = new Tree<Integer>(); 
}

What might I have done wrong?

Here is error message:

Incompatibile types:
required: structures.trees.Tree<java.lang.Integer&开发者_如何学Gogt;
found: <T>structures.trees.Tree<T>

Now the weirdness: You can try this for yourself. This code does work with my Eclipse 3.6 Helios, but it doesn't with my NetBeans 6.9.1. I can't believe it's IDE's issue (I am even using same JDK for both) ... An ideas? :O


BalusC comment - javac bug.

Solved by explicit typing:

Tree<Integer> tree = Tree.<Integer>newInstance();
0

精彩评论

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