• 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
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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: 22862
132
Eclipse IDE Spring TypeScript Quarkus 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: 22862
132
Eclipse IDE Spring TypeScript Quarkus 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: 22862
132
Eclipse IDE Spring TypeScript Quarkus 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.
 
ice is for people that are not already cool. Chill with this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic