• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generic method question

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a recent example I read I was trying to figure out what the different parts of this line were:

public static <T> List<T> backwards(List<T> input);

Does this line say that it can take a type <T> as an argument and must return at List<T>, or am I misinterpreting this method?

If this is not enough information to answer my question I am glad to list the entire example question.

Thanks,
brian
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


You got it correctly. You must return List<T>, not List<String> or
List<Object>.


Thanks,
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or, put a different way, the generic type of the formal parameter determines the generic type of the return value. So if you passed a reference to a List<String> to the backwards method it will return a List<String>, if you passed a reference to a List<Rectangle> to the backwards method it will return a List<Rectangle>.
 
Brian Deer
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So why the <T> before the List<T> return type?

public static <T> List<T> backwards(List<T> input);

[ July 01, 2007: Message edited by: Brian Deer ]
Is it a type parameter that must be used if you don't put a type parameter in the class declaration?
[ July 01, 2007: Message edited by: Brian Deer ]
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Brian Deer:
Is it a type parameter that must be used if you don't put a type parameter in the class declaration?



Yes, it it is a type parameter declaration. It is used in the case you didn't use that parameter in the class declaration. What I mean is that it is used in one of these circumstances:
- The class is not generic, i.e. it does not have a type parameter.
- The class is generic but it does not declare that type parameter, i.e. it declares another type parameter. For instance:

- The method is static, i.e. in that case the type parameter does not apply to the method. Class' type parameters refer to a particular instantiation of a class (while static methods belong to all instantations of the class).
[ July 01, 2007: Message edited by: Sergio Tridente ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The <T> before the return type is the declaration, that this method is generic.
It also declares that T can be used as a parameter variable.
Generic methods can occur also in non-generic classes.


Usually a T is used (for "type") in non-collections and an E (for "element") in collections.
But this is only a name convention, you may use any legal identifier.

For clarification: instead of
public static <T> List<T> backwards(List<T> input);

you may equally type
public static <Daisy> List<Daisy> backwards(List<Daisy> input);

but in that case be sure that you don't use a class named Daisy or you get problems.


Yours,
Bu.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic