Code explanation in Java

generics, java

Solution

You have bumped into "generics". They are explained very nicely in this guide.

In short, they allow you to specify what type that a storage-class, such as a `List` or `Set` contains. If you write `Set<String>`, you have stated that this set must only contain `String`s, and will get a compilation error if you try to put something else in there:

Set<String> stringSet = new HashSet<String>();
stringSet.add("hello"); //ok.
stringSet.add(3);
      ^^^^^^^^^^^ //does not compile

Furthermore, another useful example of what generics can do is that they allow you to more closely specify an abstract class:

public abstract class AbstClass<T extends Variable> {

In this way, the extending classes does not have to extend `Variable`, but they need to extend a class that extends `Variable`.

Accordingly, a method that handles an `AbstClass` can be defined like this:

public void doThing(AbstClass<?> abstExtension) {

where `?` is a wildcard that means "all classes that extend `AbstClass` with some `Variable`".

Problem

this morning I came across this code, and I have absolutely no idea what that means. Can anyone explain me what do these `<T>` represent? For example: ``` public class MyClass<T> ... some bits of code then private Something<T> so; private OtherThing<T> to; private Class<T> c; ``` Thank you

Original source