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 Generic method declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply 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, How does the compile treat the following 2 generic methods ?
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6588
    
    1

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


SCJP 6 articles - SCJP 5/6 mock exams - SCJP Mocks - SCJP 5 Mock exam (Word document ) - SCJP 5 Mock exam in Java.Inquisition format
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
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

Joined: Feb 24, 2006
Posts: 6588
    
    1

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.
Mohamed Sanaulla
Bartender

Joined: Sep 08, 2007
Posts: 2925
    
  15

Is Type Erasure what you were looking for?


Mohamed Sanaulla | My Blog
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
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

Joined: Sep 08, 2007
Posts: 2925
    
  15

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

Joined: Oct 24, 2010
Posts: 162
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

Joined: Sep 08, 2007
Posts: 2925
    
  15

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


int, double, String ...
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
We cannot pass int, double, String as arguments because show() method doesn't take any parameters
Mohamed Sanaulla
Bartender

Joined: Sep 08, 2007
Posts: 2925
    
  15

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

Joined: Oct 24, 2010
Posts: 162
I don't understand whether you are asking or saying. Please provide full sentence with complete meaning.
Mohamed Sanaulla
Bartender

Joined: Sep 08, 2007
Posts: 2925
    
  15

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

Joined: Oct 24, 2010
Posts: 162
They all work fine because method has generic type. My question is whole different. Let me put it in this way.
Mohamed Sanaulla
Bartender

Joined: Sep 08, 2007
Posts: 2925
    
  15

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

Joined: Oct 24, 2010
Posts: 162
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic method declaration
 
Similar Threads
Majji exam1 Qno2
What's the reason ?
Method Invocation. Generic Methods
Generics
A bunch of questions on different topics