aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic method declaration 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 "Generic method declaration" Watch "Generic method declaration" New topic
Author

Generic method declaration

Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Hi, What is the difference between the first 2 methods in this class..Also, For a method like addAll2() doesn't need Angle brackets for return type "T" ?
Rahul Saple
Ranch Hand

Joined: Aug 02, 2006
Posts: 46

The first method(like the third) is a class which makes use of generics. It uses the type parameter 'T' of the generic class Sponsor.

The second method is a generic method which defines its own type parameter 'T'(which is different than the 'T' defined by the class), and thus hides the original 'T'.

Since the third method isn't a generic one, it doesn't define a Type parameter at the start. The 'T' type used by the method is of the generic class and not its own.
Hope it helped.
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Hi Rahul, I am clear with first 2 method. How come the 3 rd method is not generic and what is the use of it ? Type parameter T is defined know ? Could you please elaborate it ?
Rahul Saple wrote:
Since the third method isn't a generic one, it doesn't define a Type parameter at the start. The 'T' type used by the method is of the generic class and not its own.
Hope it helped.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

The 3rd method uses the generic type T defined at the class level (just like the first method). The <T> syntax is used to create a new generic type for the method (in the 2nd method the return type is void, the <T> declaration creates a new generic type)...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Thanks Ankit & Rahul
Ausan Abdulhabib
Ranch Hand

Joined: Apr 25, 2010
Posts: 65
Hi all,

Can any one elaborate more for difference between

this methods :
public void addAll(T i1, T i2) {
}

and this:
public <T> void addAll1(T i1, T i2) {
}

As I need to understand, thanks




OCPJP/SCJP 6.0
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18981
    
  40

Ausan Habib wrote:Hi all,

Can any one elaborate more for difference between

this methods :
public void addAll(T i1, T i2) {
}

and this:
public <T> void addAll1(T i1, T i2) {
}

As I need to understand, thanks



Can you elaborate what you don't understand? ... as it looks like what you are asking for has been clearly explained.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ausan Abdulhabib
Ranch Hand

Joined: Apr 25, 2010
Posts: 65
public void addAll(T i1, T i2) { }

In the above method, although I not add the tag <T> before the void, but still I can use the generics T in the methods arguments.

For the below method when I add the tag <T> so this mean, I can use generics T in the methods arguments.
public <T> void addAll1(T i1, T i2) { }

I want to understand what is the difference if I add <T> before void or not add it.
I read in the book K&B in page 628 as the following:
The <T> before void simply defines what T is before you use it as a type in the
argument. You MUST declare the type like that unless the type is specified for the
class. In CreateAnArrayList, the class is not generic, so there's no type parameter
placeholder we can use.
But still confuse, kindly need help to understand this issue.
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
In the case of, public void addAll(T i1, T i2) { } parameter T is defined in class level. as class Sponsor<T>
In the case of public <T> void addAll1(T i1, T i2) { } parameter T is defined in method level before void. To do that, use <T> before void..Angle brackets plays a role. If <T> is declared both in class level and method level, you will get a compiler warning at method level. Warning is : The type parameter T is hiding the type T. Please revert back if you are not clear.

Ausan Abdulhabib
Ranch Hand

Joined: Apr 25, 2010
Posts: 65
Yes it is clear. Thank you very much for your explination.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic method declaration