0

Can you please explain to me why am I getting this error when trying to define?

a generic class:

public class GenericClass<T, S extends T> {
    ...
}

Main.java:

public class Main(){
     public static void main(String[] args){

         // This line generates compilation error
         GenericClass<Number, Integer> object = new GenericClass();
     }
}

I am getting this error:

Bound mismatch: The type Integer is not a valid substitute for the bounded parameter <S extends T> of the type GenericClass<T,S>

[SOLVED]

I had another class in my project called Integer :)

Thank you very much for quick response

Owen Pauling
  • 11,349
  • 20
  • 53
  • 64
Cristof
  • 33
  • 1
  • 5
  • 5
    What error? Please [edit] the question and include it. – Jorn Vernee May 28 '17 at 10:08
  • Are you considering, *Unchecked assignment of GenericClass to GenericClass* as a compiler error? – Naman May 28 '17 at 10:21
  • 1
    You are using a raw type on the right side of the `=`. See [What is a raw type and why shouldn't we use it?](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – Jesper May 28 '17 at 14:42
  • Just updated the question. – Cristof May 28 '17 at 14:52
  • Don't ever use the same name as a common standard API type for a custom type. You should consider names from `java.lang` and `java.util` off limits. – Lew Bloch May 28 '17 at 15:30

1 Answers1

3

This code compiles and runs fine.

There's no compilation error, however as explained by nullpointer, there's a warning:

Unchecked assignment of GenericClass to GenericClass<java.lang.Number, java.lang.Integer>

An easy way to fix this is to replace your code line with this:

// Java 6 and lower
GenericClass<Number, Integer> object = new GenericClass<Number, Integer>();

// Java 7 and higher, using the diamond operator
GenericClass<Number, Integer> object = new GenericClass<>();
Sir4ur0n
  • 1,753
  • 1
  • 13
  • 24