File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes instanceof problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "instanceof problem" Watch "instanceof problem" New topic
Author

instanceof problem

Thomas Peter
Greenhorn

Joined: Oct 02, 2010
Posts: 4
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

Joined: Jul 26, 2007
Posts: 10376
    
    8

Welcome to the Ranch.

In future, while posting code, please UseCodeTags

You should be invoking the instanceof operator like
and not


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Thomas Peter
Greenhorn

Joined: Oct 02, 2010
Posts: 4
... 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

Joined: Nov 04, 2009
Posts: 2066

I think, we can do, if we are able to modify the Class which you passed as argument to the method.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10376
    
    8

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

Joined: Nov 04, 2009
Posts: 2066

I think,


will work!
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Oh, already answered!~
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4543
    
    5

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())


luck, db
There are no new questions, but there may be new answers.
Thomas Peter
Greenhorn

Joined: Oct 02, 2010
Posts: 4
Thank you all !

The 'isInstance' helps a lot :-)

Cheers freefalling
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Oct 02, 2010
Posts: 4
;-)

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instanceof problem