File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Generics Question. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generics Question." Watch "Generics Question." New topic
Author

Generics Question.

Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 119
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.
lokesh sree
Ranch Hand

Joined: Oct 27, 2009
Posts: 100

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.


Lokesh
( SCBCD 5, CCENT, SCJP 5 )
Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 119
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.
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

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:

Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1158
    
    1



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



Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I
Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 119
Hi!

Thanks for your replies.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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:


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

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
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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??
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics Question.