12

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(

Martin Meeser
  • 2,061
  • 2
  • 19
  • 36
  • possible duplicate of [Class is a raw type. References to generic type Class should be parameterized](http://stackoverflow.com/questions/1372595/class-is-a-raw-type-references-to-generic-type-classt-should-be-parameterized) – wchargin Jun 06 '13 at 01:20
  • 4
    Please do not edit your question to invalidate answers. If you have a new problem, open a new question. Since you've removed the original question and replaced it with a new one, my answer no longer makes sense. I have rolled back the question. – cdhowie Jun 06 '13 at 01:27
  • Sorry, I will open a new question. –  Jun 06 '13 at 01:30
  • No problem, everyone is new once. :) – cdhowie Jun 06 '13 at 01:47

2 Answers2

22

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.

cdhowie
  • 133,716
  • 21
  • 261
  • 264
  • Thanks cdhowie. I am sorry, I had several diff things I was trying. I am having a more stubborn error edited now. Thanks for your input! –  Jun 06 '13 at 01:28
0

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)

crifan
  • 8,424
  • 1
  • 45
  • 35