You can only create instances of known types. T
is not something that comes with Java, but it is often used as an internal type parameter in a class that takes a type parameter (java generics).
As an example, say that you are creating a type parameterized linked list, that is, a linked list that you can use with any one data type as contents. If you would want an instance of this class that could only contain String
s, you would instantiate it like so:
MyLinkedList<String> list = new MyLinkedList<String>();
The definition of such a class needs a type parameter, that will represent whatever class it was instantiated with, and would look something like this:
class MyLinkedList<T> {
// implementation code
}
Inside that class, you can create instances of type T, and you would do it with T t = new T();
, just like you'd expect. T will however refer to some other class at runtime, since T is just a type parameter.
That T
type will however not be accessible outside the class.
If you're not implementing a type parameterized class, you would have to create a class of type T in order to instantiate it, and make sure to include it into whatever scope you want to use it in. I wouldn't recommend doing that though, because it Isn't very descriptive.