Domanda

Possible Duplicate:
What does the <TYPE> in java mean?

Hello I came across this class while debugging , can someone give me pointers to what it means, please. Thanks.

class Something<P>{
 private P someVariable;
}

//what does <P> mean here? 

Thanks.

È stato utile?

Soluzione

This is a generic. It allows you to write code that works with different types.

Try this tutorial:

http://docs.oracle.com/javase/tutorial/java/generics/

Altri suggerimenti

It means its a generic class. You create a generic type declaration by changing the code

  "public class Box" to "public class Box<T>"

For further information you can see this reference: http://docs.oracle.com/javase/tutorial/java/generics/types.html

This is an example of class templating (although it is erased at runtime). Usually it is class and not class

. It allows you to inject a type into a class at compile time.

For example if you did

new Something<String>();

then the someVariable would be of type String.

If you called

new Something();

then I believe someVariable would be of type Object as it would have no inferred type information. Usually your IDE will give you a warning about this.

It is also described here.

P is a type used for generics.

Usually it is T, or TEntity, for type or entity type.

Just think of ArrayList<string> as an example where the type is string.

This is a Generic class definition.

<P> is the place holder for an Object that get substituted at compile.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top