• 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 declaration

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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" ?
 
Ranch Hand
Posts: 46
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)...
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit & Rahul
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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



 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ausan Abdulhabib
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it is clear. Thank you very much for your explination.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic