• 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

doubt - generics in method ...

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly do you think is wrong with this?
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic