• 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, How does the compile treat the following 2 generic methods ?
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The generic that is defined is never really used. What do you mean by 'how does the compiler treat it ?'
 
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
How does the compiler/we can convert this generic style code methods in to non-generic style code methods ?
For example, Define class : class Animal<T> {}
Create Instance: Animal<Cat> ref = new Animal<Cat>();
The compile/we can treat/assume the class as class Animal<Cat>{} instead of class Animal<T> {}

Deepak Bala wrote:The generic that is defined is never really used. What do you mean by 'how does the compiler treat it ?'

 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saibabaa Pragada wrote:How does the compiler/we can convert this generic style code methods in to non-generic style code methods ?
For example, Define class : class Animal<T> {}
Create Instance: Animal<Cat> ref = new Animal<Cat>();
The compile/we can treat/assume the class as class Animal<Cat>{} instead of class Animal<T> {}

Deepak Bala wrote:The generic that is defined is never really used. What do you mean by 'how does the compiler treat it ?'



How does a compiler treat the generic declaration 'T' internally ? I can guess but I am not sure how that spec is implemented.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is Type Erasure what you were looking for?
 
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
I am trying to understand how these 2 generic methods can be written as Non-generic methods.

mohammed sanaullah wrote:Is Type Erasure what you were looking for?

 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saibabaa Pragada wrote:I am trying to understand how these 2 generic methods can be written as Non-generic methods.

mohammed sanaullah wrote:Is Type Erasure what you were looking for?



In the show() I dont see the Type T being used. Did you try passing some other data types for the method getResult?
 
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
1. In the show() I dont see the Type T being used - Yes. It is not being used.
2. Did you try passing some other data types for the method getResult? --> Yes. I am giving code snippet again.
Did I provide what you are looking for ?
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saibabaa Pragada wrote:
Did I provide what you are looking for ?



int, double, String ...
 
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
We cannot pass int, double, String as arguments because show() method doesn't take any parameters
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saibabaa Pragada wrote:We cannot pass int, double, String as arguments because show() method doesn't take any parameters



For the one which takes arguments.
 
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
I don't understand whether you are asking or saying. Please provide full sentence with complete meaning.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saibabaa Pragada wrote:I don't understand whether you are asking or saying. Please provide full sentence with complete meaning.



Sorry for that. What I meant was- With the generics in place- You can pass values of different data types to the method- getResult. I wanted you to try this in the code. After the String passing, you could add method calls to getResult which would pass an int or a double and so on.
 
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
They all work fine because method has generic type. My question is whole different. Let me put it in this way.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saibabaa Pragada wrote:They all work fine because method has generic type. My question is whole different. Let me put it in this way.



The <T> before the return type indicates that the Type T being used here is not related to the ones used in the class. A class can also have a generic declaration with Type T. Is this what you were looking for?
I remember someone reply to the similar in another post.
 
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
I replied the answer for somebody's post. What I am thinking is,

1. static <T> void show() { // What is the use of <T> after static and before void? --> Doesn't make any sense to me..Not useful.
2. static <T> void getResult(T obj) { //What is the use of <T> after static and before void ? --> <T> should be defined to use T in the method parameter. If not Compiler error. <T> "T cannot be resolved to a type". I don't know the reason

Please advice if anybody has reasons for these 2 points.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic