简体   繁体   中英

Raw type. References to generic types should be parameterized

I have a Cage class:

public class Cage<T extends Animal> {
// the construtor takes in an integer as an explicit parameter
...
}

I am trying to instantiate an object of Cage in another class main method:

private Cage cage5 = new Cage(5);

I get the error: Cage is a raw type. References to generic type Cage should be parameterized. I tried several ideas, but am stuck about this tricky syntax :o(

Cage<T> is a generic type, so you need to specify a type parameter, like so (assuming that there is a class Dog extends Animal ):

private Cage<Dog> cage5 = new Cage<Dog>(5);

You can use any type that extends Animal (or even Animal itself).

If you omit the type parameter then what you wind up with in this case is essentially Cage<Animal> . However, you should still explicitly state the type parameter even if this is what you want.

For other java newbie like me.

  • Code is look like this:
public class ContinuousAddressBuilder<T> extends VariableLengthPacket {
  ...

  /* T=int/float/double */
  private ArrayList<T> informosomes;

  ...

  public ContinuousAddressBuilder builderCon(int con) {
    ...
  }
}
  • Solution:

Add <T> after your class:

change from

public ContinuousAddressBuilder builderCon(int con)

to

public ContinuousAddressBuilder<T> builderCon(int con)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM