• 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

Generics : passing explicit type information for constructor level type parameter

 
Ranch Hand
Posts: 48
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Rakesh K. Cherukuri
Ranch Hand
Posts: 48
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 67
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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>")
 
Rakesh K. Cherukuri
Ranch Hand
Posts: 48
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 67
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 48
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Rakesh,
Yes. The constructor can take any E type. It will compile:

or


and etc ....
 
Rakesh K. Cherukuri
Ranch Hand
Posts: 48
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic