Help coderanch get a
new server
by contributing to the fundraiser
  • 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

generic casting with object.getClass() ?

 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have something a generic class:



In MyBean, I instantiate this generic class to a object of type 'Hobby':





How can I solve way 2 or way 3 ?
I cannot use way 1, as I have a generic method in which I need to put the cast explicitly by its given object-instance.
 
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
Just change the reference from MyObject<?> o1 to MyObject<Hobby> 01 or MyObject<? super extends Hobby>
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Just change the reference from MyObject<?> o1 to MyObject<Hobby> 01 or MyObject<? super Hobby>



Ineed, I can do that - But when doing this:




I can use this method only for objects of type Hobby and this is what I want to avoid !

I want to use this method for any type of MyObject.

I want to have something like an abstract generic Method which can be invoked from any type of MyObject.
 
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
In my previous post I used super when I meant extends. I edited my post. When you use ? extends Hobby then you can use any class as long as it extends Hobby.
 
Sheriff
Posts: 22791
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nimo frey wrote:I can use this method only for objects of type Hobby and this is what I want to avoid !

I want to use this method for any type of MyObject.


Apparently not, or you wouldn't have the cast to Hobby in there.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Apparently not, or you wouldn't have the cast to Hobby in there.



Yes, I want to cast it to Hobby, but not always.

In other cases, I want cast it to, for example, Object of type 'Work'.

Imagine, I have this method and would cast the object 'o' to 'object_type':



So you see,
I want to avoid the if-clauses and cast the object directly as I have the 'object_type'-property, which indicates which type of class my object 'o' is.

Is this possible? Is there another better solution?
 
Rob Spoor
Sheriff
Posts: 22791
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class has a method called cast but that will only make sense if you know the Class type. For instance, with a Class<Hobby> object "cls", cls.cast(x) will either return x cast to Hobby or throw an exception.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed,

I can do the cast via:




but that will only make sense if you know the Class type



Unfortunately, I cannot cast something like:



So the only solution is to use the if-clauses.

By the way, where lies the difference of

and


(faster?)
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, I found the solution:


According to this:


http://www.velocityreviews.com/forums/t145694-p2-class-cast-object-what-benefit.html

I am able to do the cast this way:





So Bob, you are right, cls.cast will do the cast.
 
Rob Spoor
Sheriff
Posts: 22791
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And check if the object is an instance (or null) at the same time, throwing a class cast exception if it isn't.
 
Destiny's powerful hand has made the bed of my future. And this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic