• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instanceof problem

 
Thomas Peter
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'd like to test if a given Component is an instance of a given class.
Instead of having several methods like this ...

I thought I could use an universal one ...


But that doesn't compile. Any ideas how I could accomplish that task ?

cheers freefalling
 
Maneesh Godbole
Saloon Keeper
Posts: 11342
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

In future, while posting code, please UseCodeTags

You should be invoking the instanceof operator like
and not
 
Thomas Peter
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... thanks but I guess that's not what I'm looking for ;-)

I wanted to call a method like that



I want a method that doesnt check just one hardcoded class like


but a variable class



Thanks freefalling
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, we can do, if we are able to modify the Class which you passed as argument to the method.
 
Maneesh Godbole
Saloon Keeper
Posts: 11342
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Peter wrote:... thanks but I guess that's not what I'm looking for ;-)
...I want a method that doesnt check just one hardcoded class like...
...but a variable class..


In that case check out Class#isInstance(Object obj)
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think,


will work!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, already answered!~
 
Darryl Burke
Bartender
Posts: 5149
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need something like (typed here, may have typos or other bugs -- read the API for Class#isAssignableFrom)Note that class is a keyword. You can't use it as a variable or parameter identifier.

edit Oops, was I really on the phonefor so long? isInstance is a better choice. Also, I got isAssignableFrom backwards should have been clazz.isAssignableFrom(co.getClass())
 
Thomas Peter
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all !

The 'isInstance' helps a lot :-)

Cheers freefalling
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I'll ask what nobody else has: why do you want to do this? One of the advantages of OOP is that we really shouldn't need to do this kind of operation.
 
Thomas Peter
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
;-)

I almost expected that question. My problem is having programmed a long time in VB and now I want to learn how the things can be done in Java.

Explicitly I tried to change one JButtons label from another method. Or try to access a component directly by its name ...

Thanks freefalling
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic