File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt - generics in method ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt - generics in method ..." Watch "doubt - generics in method ..." New topic
Author

doubt - generics in method ...

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Aug 31, 2004
Posts: 11343

What exactly do you think is wrong with this?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
<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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Oct 11, 2004
Posts: 3830
or rather , first one is not generic method , it is just taking a generic parameter ...

Is it right ?
Thanks .
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt - generics in method ...