What does class

mean in Java? [duplicate]

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.

0

5 Answers

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

Try this tutorial:

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:

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.

2

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.
&ltP&gt is the place holder for an Object that get substituted at compile.

You Might Also Like