Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt - generics in method ...

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also create generic methods
-----------------------------------


This code I found in one tutorial , I think this is wrong ...
Can any body help ..
Thanks .
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly do you think is wrong with this?
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<U extends List<? extends Number>>

I think , this is not at right position ...

How can we call this method ??

like any simple method :
public void m(int a) { ....... }

any generic method may be like this :
public void m(List<String> listOfString) { ......... }

please help me in this ...
Thanks a lot .
[ March 02, 2005: Message edited by: rathi ji ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just as a class can be made generic by specifying parameter types in the class definition, a method can be made generic by specifying paramter types in the method definition. In this case, the type parameter is specified just prior to the return type:

public <T> void meth2(T t) {}

Note that generic methods can be used within non-generic classes.

For example, the class Foo is not generic. However, it contains a generic method called iter. The argument to iter is a parameterized type defined as:

<U extends List<? extends Number>>

This means that the argument must be some kind of List (List or a subclass of List), with the List itself parameterized to contain some kind of Number (for example, Integer).
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Marc ,

It means , there are two flavor of generic method :

public void m(List<String> listOfString) { ......... }

In this case , a paramaterized String list or a non paramerized list can be passed in the method ...

public <T extends MyClass> void m(T t) { ......... }

In this case , any object of MyClass or its subclass can be passed into this method ...

Is everything right ???
please reply ..
Thanks a lot


public <T> void m(T t) { .. }
public <T extends Object> void m(T t) { .. }

This two method are equivalent ..
Am I right ?
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or rather , first one is not generic method , it is just taking a generic parameter ...

Is it right ?
Thanks .
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As explained by Herbert Schildt on page 45 of his book, Java 2 v5.0 (TIGER) New Features,...
...methods inside a generic class can make use of a class' type parameter and are, therefore, automatically generic relative to the type parameter.

I take this to mean that a "generic method" might simply take a parameterized argument, without defining its own type parameter. The difference is basically the scope of the type parameter -- whether it pertains to the entire class or is local to the method.
[ March 02, 2005: Message edited by: marc weber ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic