1

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>
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

Xorty
  • 18,367
  • 27
  • 104
  • 155
  • 1
    Duplicate of [Generics compiles and runs in Eclipse, but doesn't compile in javac](http://stackoverflow.com/questions/2858799/generics-compiles-and-runs-in-eclipse-but-doesnt-compile-in-javac). It's a [bug](http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6468354) in javac. – BalusC Oct 24 '10 at 22:26
  • 1
    By the way, you forgot `static` in the `newInstance()` method. – BalusC Oct 24 '10 at 22:29
  • Can't you just write the method as `public static Tree newInstance()` and it will automatically fill in the `T` from the class declaration? – Tyler Jun 21 '11 at 20:16

1 Answers1

2

BalusC comment - javac bug.

Solved by explicit typing:

Tree<Integer> tree = Tree.<Integer>newInstance();
Xorty
  • 18,367
  • 27
  • 104
  • 155