aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics : passing explicit type information for constructor level type parameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics : passing explicit type information for constructor level type parameter" Watch "Generics : passing explicit type information for constructor level type parameter" New topic
Author

Generics : passing explicit type information for constructor level type parameter

Rakesh K. Cherukuri
Ranch Hand

Joined: Jun 01, 2010
Posts: 48

Hi,

While looking at the generics mechanism ended up writing the following code.

To me its a bit of surprise that it works fine.

My thought : In line 1, since the code specified the type value for E as String, the compiler should have thrown an error when it found Long instance in the constructor.

Am i missing something here ?

Warm Regards,
Rakesh
Rakesh K. Cherukuri
Ranch Hand

Joined: Jun 01, 2010
Posts: 48

Just realized : perhaps, the code takes String type for type parameter T and not E.

Is there a way to specify type value for E explicitly ?
Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67

Rakesh K. Cherukuri wrote:
Is there a way to specify type value for E explicitly ?


Yes you can specify type value explicitly before method and constructor.

Rakesh K. Cherukuri wrote:



The above line should be below to compile. (Generics comes after class name "clasName<Generic>")


OCPJP 6 (91%)
Rakesh K. Cherukuri
Ranch Hand

Joined: Jun 01, 2010
Posts: 48

Thanks for the reply Ankita. Type values can be specified before or after the constructor name. Both ways it compiles fine.

Rakesh K. Cherukuri wrote:Is there a way to specify type value for E explicitly ?


Guess i should have been more clear. The question i had in mind is

Is there a way to specify type value explicitly for E, which in the reported code, defined at constructor level and not at class level.
Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67

Hi Rakesh
Rakesh K. Cherukuri wrote:
Is there a way to specify type value explicitly for E, which in the reported code, defined at constructor level and not at class level.


The type value for E will be get at runtime , I don't think so there are any way to provide explicit value for E which defined at constructor level and not at class level , I am not sure we can give value it to compile time.

and
Rakesh K. Cherukuri wrote:
Type values can be specified before or after the constructor name. Both ways it compiles fine.


when you define constructor or method at that time Generic type should be come before constructor or method return type, otherwise it gives compiler error.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 758
Hi Rakesh Buddy,

In KB's book , there is a section about declaring generic type in constructor just like your program.
First of all, in this code: SomeGenericClass<String> i = new <String>SomeGenericClass ("adsf", new Long(10)); T is the String. The Long object is the E.
The compiler does not know what E type is until you pass in the Long. Try this :
SomeGenericClass<String> i = new <String>SomeGenericClass ("adsf", new Double(10));
It compiles.

Rakesh K. Cherukuri wrote:Hi,

While looking at the generics mechanism ended up writing the following code.

Rakesh K. Cherukuri
Ranch Hand

Joined: Jun 01, 2010
Posts: 48

Thanks for taking time out Minh.

Himai Minh wrote:
First of all, in this code: SomeGenericClass<String> i = new <String>SomeGenericClass ("adsf", new Long(10)); T is the String. The Long object is the E.

Yes, thats true.

Himai Minh wrote:
The compiler does not know what E type is until you pass in the Long. Try this :


This is where my question was/is. Is it possible to pass a value type for E explicitly given that its at constructor level?
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 758
Hi, Rakesh,
Yes. The constructor can take any E type. It will compile:

or


and etc ....
Rakesh K. Cherukuri
Ranch Hand

Joined: Jun 01, 2010
Posts: 48

I am afraid my question was not clear again.

Let me put it this way. There is type-value passing and there is value passing.

In above code,
As far as t is concerned : type-value is passed as "String" for T and "asdf" is passed as value.
As far as e is concerned : type-value is not passed explicitly for E and an Integer instance is passed as value.

Its true that at runtime java inference will be able to make out that E is of type Integer. But thats off the topic. My question : how to pass value type (Integer in the above code) for E explicitly.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Generics : passing explicit type information for constructor level type parameter