• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Generics Question.

 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

What is




What does mean? Is it returning a void of type ?

I read from here:

http://download.oracle.com/javase/tutorial/java/generics/genmethods.html

Thanks.
 
Ranch Hand
Posts: 100
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
That syntax seem to be explained in the tutorial itself whose link is given.
In case if that is not clear, in
the return type there is still "void". The syntax there. is a way of specifying "U" as a generic type scoped for that method inspect().
This syntax of declaring a generic type just before specifying the return type is an alternate to specifying the generic type with the class , as done with Box T; there. If you look at the class there, it is defining "T" as generic type but that is scoped for the whole class. Where as "U" is a generic type which can be used only in inspect().

Hope that clears your confusion.
 
Alan Blass
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

lokesh chenta wrote:it is defining "T" as generic type but that is scoped for the whole class. Where as "U" is a generic type which can be used only in inspect().



Hi! Why do you need to specify T as generic type scoped for the whole class since T is already private and is available for the whole class?

or alphabet "YOU" for method inspect() since it is already understood it is for the method's scope.

I mean why do you need to specify it?

Thanks.
 
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alan Blass wrote:

lokesh chenta wrote:it is defining "T" as generic type but that is scoped for the whole class. Where as "U" is a generic type which can be used only in inspect().



Hi! Why do you need to specify T as generic type scoped for the whole class since T is already private and is available for the whole class?

or alphabet "YOU" for method inspect() since it is already understood it is for the method's scope.

I mean why do you need to specify it?

Thanks.



When declaring a generic you need to specify the input parameters to the generic as you do with everything else. Since the generic declaration (in a metaphorical way) can be viewed as a template, you need to declare how and when to specify the parameters for the generic construction.

When using a type wide generic parameter (<T>) you are declaring that "this input parameter needs to be declared when creating a new type". That is when you declare an attribute/variable or when you use new.

When using a method scoped generic, you tell it that "when you call this method, tell me what type I should use and I will create that method".

An example on the later:


This tells us that I have declared a method that takes a type with the name T. If I call this method like this:


the compiler/jvm will write me this method:

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


as mentioned earlier, 'P' define the genertic type and (P p) within method parameter defines an instance of generic type 'P'.

alphabet "YOU" for method inspect() since it is already understood it is for the method's scope.


this understanding is not correct. you will have to define 'P' as generic type before using it any where in class/method. This information is required by compiler.

Let us study below mentioned code sample. Here I have used a generic method in a non generic class.



Here, output is

p => Test A object
p => Test B object


If I remove 'P' defined before void method


then I will get compile time error as
The method inspect(P) from the type Testing refers to the missing type P

We got this error as generic type 'P' is used in method parameters but not defined any where.

To fix this error there are two ways
1. define generic type 'P' at method level (which was there already)


2. define generic type 'P' at class level (as shown below). Testing class has been added generic type 'P'



So by now you must have understood that to use generic type paramters variables etc , we ned to define that generic type at method or class level.

==================================================================================================

Why do you need to specify T as generic type scoped for the whole class since T is already private and is available for the whole class?

This understanding is not correct. T is not private to class. Reason for specifying 'T' as generic type explicitly is to tell compiler that we are generic reference which shall be replaced by actual implementation class later on.

A sample generic class is shown below



Now if we instantiate this generic class as

then generic type 'T' will be replaced with actual implementation class Integer

So inside JVM this class will become




Similiarly if we instantiate this generic class as

then generic type 'T' will be replaced with actual implementation class String


~ abhay

 
Alan Blass
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

Thanks for your replies.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ove Lindström wrote:
the compiler/jvm will write me this method:


No. Type information is only available at compile time. At runtime all the type information is no longer available due to type erasure. This process is there to maintain backwards compatibility.



Therefore it is legal to do:
 
Ove Lindström
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wouter Oet wrote:
Therefore it is legal to do:



Well, yes, but we could argue if it is legal or not when it throws you an error... ;)

Don't confuse the young ones.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ove Lindström wrote:Well, yes, but we could argue if it is legal or not when it throws you an error... ;)


It throws an Exception not an Error. And the compiler accepts it so in my book that is legal. If it is correct depends on the situation.

Ove Lindström wrote:Don't confuse the young ones.


You provided incorrect information, I corrected you, provided an example in which I clearly marked where you get the warnings and where the Exceptions and I'm confusing the young ones??
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic