It says in the book (APress Ganesh / Sharma) about Java 7:

You create a method named fill() in the Utilities class with this declaration:

public static <T> void fill(List<T> list, T val)

You declare the generic type parameter T in this method. After the qualifiers public and static, you put <T> and then followed it by return type, method name, and its parameters. This declaration is different from generic classes—you give the generic type parameters after the class name in generic classes.

But why <T> as in public static <T> ? The syntax looks odd and appears nowhere else in the Java language. What does the <T> mean in this case and why is it used?


2 Answers 2


it means only the method is generic and allows the class to be not generic (or use a generic parameter not in the class)

for example the Collection toArray method has a parameter, this will allow the type of the array to be different from the type of the collection, (handy when the Collection type is complicated)

the angle brackets is to remain backwards compatible with the existing syntax (pre 1.5), which only used it for the comparison (and it's cheated of from C++ where they faced the same problem concerning compatibility with existing code) ans this way older compilers will choke on exactly that syntax, which will indicate exactly where the problem is

so they deliberately choose the syntax because it was used nowhere else in the language

the location in the declaration before the return type is to not confuse the compiler when a raw type is used (like is public List <T> foo(T t) a method returning List<T> or a generic method returning a raw List)

the calling syntax Class.<T>foo() is again to avoid confusing the compiler; the < after . can only mean the start of a generic qualifier, while putting it after the method name can be ambiguous:

is foo(MyClass.bar<T,Y>(ab)) calling a single parameter foo using the return type of MyClass.bar<T,Y> or a foo that takes 2 booleans, you need to know whether MyClass.bar is a field or a method


From a 1995 interview with Alexander Stepanov in Dr Dobb's Journal: "I returned to generic library development in 1992 when Bill Worley, who was my lab director established an algorithms project with me being its manager. C++ had templates by then. I discovered that Bjarne had done a marvelous job at designing templates. I had participated in several discussions early on at Bell Labs about designing templates and argued rather violently with Bjarne that he should make C++ templates as close to Ada generics as possible. I think that I argued so violently that he decided against that. I realized the importance of having template functions in C++ and not just template classes, as some people believed. I thought, however, that template functions should work like Ada generics, that is, that they should be explicitly instantiated. Bjarne did not listen to me and he designed a template function mechanism where templates are instantiated implicitly using an overloading mechanism. This particular technique became crucial for my work because I discovered that it allowed me to do many things that were not possible in Ada. I view this particular design by Bjarne as a marvelous piece of work and I'm very happy that he didn't follow my advice."

In Ada, a generic must be instantiated, as for example:

  package IntIo is new TextIo.IntegerIo(int);
  package coerce is new UncheckedConversion(SomeTypeA, SomeTypeB);

In C++, Stroustrup set it up so you write something like:

  ObjectOfTypeB := coerce<SomeTypeA,SomeTypeB>(ObjectOfTypeA);

Stroustrup had to use SOMETHING to indicate the generic and its specialization. He chose to overload angle brackets. This initially created problems. What if your template specialization required another template specialization, and you needed to close them both? The lexer sees two closing >, or >>, and recognizes that as a shift operation. A later language fix was required, to overload the shift operators << and >>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.