0
public class A<B> {
    public func() {
        B b = new B();
    }
}

I get this error hint from Netbeans:

unexpected type
  required: class
  found:    type parameter B
  where Bis a type-variable:
    B extends Object declared in class A

How can I new a object of class B?

Yishu Fang
  • 9,448
  • 21
  • 65
  • 102
  • Share a complete code and in what line the error happens? compile time or execution time? – DiogoSantana Mar 24 '13 at 02:48
  • possible duplicate of [Create instance of generic type in Java?](http://stackoverflow.com/questions/75175/create-instance-of-generic-type-in-java) See also: [How can I instantiate a generic type in Java?](http://stackoverflow.com/questions/6916346/how-can-i-instantiate-a-generic-type-in-java) – Paul Bellora Mar 24 '13 at 02:49

4 Answers4

2

Due to type erasure you cannot make it like that. Take a look at the following answer https://stackoverflow.com/a/6810709/860294

Community
  • 1
  • 1
João Fernandes
  • 1,101
  • 5
  • 11
2

B in A<B> is just a type parameter. You cannot create an instantiate a parameter. You can only create an instance of actual type.

But you can declare the method to take that instance as an argument public func(B b).

I recommend you to read the tutorial on generics.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
  • 2
    It's important to realize this is because of type erasure in Java. It's how Java generics were implemented. At runtime Java B is always of type Object, but the compiler shoves in the casts that you needed automagically. Java made this decision for backwards compatibility. In languages such as C# type information about object B is preserved. – Jazzepi Mar 24 '13 at 02:52
  • I don't think it has anything to do with type erasure. The reason is because it's not an actual type (it's just a type parameter). e.g. with `public class MyComp implements Comparator`, you can always instantiate `MyObject` in `compare` method. – Bhesh Gurung Mar 24 '13 at 02:56
  • 1
    @BheshGurung It has everything to do with type erasure. By contrast, in C# (where generic types are reified) you can do `new T()`. – Paul Bellora Mar 24 '13 at 03:00
  • @BheshGurung No, it is not really a parameter and yes, it has to do with type erasure. Jazzepi's comment is correct and educational. – João Fernandes Mar 24 '13 at 03:00
  • @PaulBellora: I think it depends on whether are talking Java only or generics in general. – Bhesh Gurung Mar 24 '13 at 04:05
  • @JoãoFernandes: It can't be an actual type. – Bhesh Gurung Mar 24 '13 at 04:06
  • "At runtime Java B is always of type Object". The "type of a variable" is a compile-time concept anyway – newacct Mar 24 '13 at 06:38
  • @newacct Type of a variable is definitely something that exists during runtime, it's NOT solely a compile-time concept. During runtime I can use instanceof to query an object's type before casting. – Jazzepi Mar 24 '13 at 11:35
  • @Jazzepi: you are talking about the class of an object, not the type of a variable – newacct Mar 24 '13 at 18:49
  • @newacct Classes are types, user defined ones. So the class of an object is the type of that object. When you write a method that returns String, you would say the "method returns a value of type String" – Jazzepi Mar 24 '13 at 20:41
  • @Jazzepi: For every class, there happens to be a reference type of the same name. Type applies only to expressions and variables at compile time. `String foo = "bar";` and `Object baz = "bar";` are variables with different type. At runtime, there is no difference. They are both references that point to objects of class `String`. If I do `bar = null;` all you see at runtime is a reference that is a null pointer. At compile time we have a concept of a variable of type `String`. If I have `char` and `short` all you see is bytes at runtime but I know they are different at compile time. – newacct Mar 25 '13 at 06:30
0

Java uses type erasure. it doesnt know the class of B, and so it doesnt know whether whatever class B ends up being at run time will have a constructor with that set of arguments.

scott_fakename
  • 10,219
  • 2
  • 15
  • 20
0

The reason for this is that B can be either an interface or a class (even Abstract one). So, new B() will not be valid in case of Interface or Abstract Class. Hence, it is not allowed.

Param
  • 2,420
  • 1
  • 14
  • 10