• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Generic Methods

 
Ranch Hand
Posts: 46
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why Test1 compiles and Test2 does not?
Also as per page number 628 of K&B, it says

"The strangest thing about generic methods is that you must declare the type variable BEFORE the return type of the method:
public <T> void makeArrayList(T t)"


Then why even 1st one is getting compiled correctly?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Test2<T> tells us that each instance of a class has a particular generic type associated with it. But then you've got a static method. This isn't associated with any particular instance. So "T" has no meaning in this context, so it can't compile.

These are generic classes. What K&B are referring to there are generic methods - where each call of the method is associated with a particular type. This has a different syntax. The following should compile:
Or even this:But this isn't a good idea, as the two Ts are actually unrelated - the class and method are both separately generic. So you'd be better writing it as something like this to avoid confusion:
 
Dishi Jain
Ranch Hand
Posts: 46
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:class Test2<T> tells us that each instance of a class has a particular generic type associated with it. But then you've got a static method. This isn't associated with any particular instance. So "T" has no meaning in this context, so it can't compile.

These are generic classes. What K&B are referring to there are generic methods - where each call of the method is associated with a particular type. This has a different syntax. The following should compile:
Or even this:



But then why the above methods would get compiled? Since they are also static methods.
However I am not getting the purpose of putting <T> before return types.


Also,
The correction mentioned is also not clear to me.
We are mentioning some other type variable in Class name while an other type variable in method.
I am only aware of basic java, so not getting this clearly.

Thanks,
Dishi
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's just that there are two completely separate ways of using generics - classes and methods - and you're getting confused between them.

In generic classes, we put the <T> after the class name. Each instance of the class then has a particular generic type. Because static methods don't belong to instances, but to the class as a whole, static classes don't know what this type is. So they can't refer to it.

In generic methods, we put the <T> before the return value of the method. Each call to the method has a particular generic type (based on whatever the arguments are). And because the generic type belongs to the method call, and not the class instance, it works with both static and non-static methods. Which is why my example should compile.
 
What are you doing? You are supposed to be reading this tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic