if (obj instanceof classVariable) ?

Discussion in 'Java' started by natG, Sep 22, 2003.

  1. natG

    natG Guest

    Hi;

    How can I check if an object is an instance of a class where the class is
    represented in a Class variable?

    boolean checkInstance(Object obj, Class c){
    if (obj instanceof c) does NOT WORK!
    }

    thanks
    -nat
    natG, Sep 22, 2003
    #1
    1. Advertising

  2. natG

    Harald Hein Guest

    "natG" wrote:

    > Hi;
    >
    > How can I check if an object is an instance of a class where the
    > class is represented in a Class variable?


    If you don't need inheritance

    if(obj.getClass().equals(c))
    Harald Hein, Sep 22, 2003
    #2
    1. Advertising

  3. natG

    Skippy Guest

    > > How can I check if an object is an instance of a class where the
    > > class is represented in a Class variable?

    >
    > If you don't need inheritance
    >
    > if(obj.getClass().equals(c))


    if(obj.getClass().toString().equals(c.toString()))

    or maybe (don't know) :

    if(object.getClass() == c)
    Skippy, Sep 22, 2003
    #3
  4. natG

    Rach Guest


    > Hi;


    > How can I check if an object is an instance of a class where the class
    > is represented in a Class variable?


    > boolean checkInstance(Object obj, Class c){
    > if (obj instanceof c) does NOT WORK!
    > }


    You can only use instance of statically, if you want to dynamically check an
    object is of a type of a class, then use

    obj.getClass().equals(clazz);

    or for inheritence,

    clazz.isAssignableFrom(obj.getClass());
    Rach, Sep 22, 2003
    #4
  5. Stefan Matthias Aust, Sep 22, 2003
    #5
  6. natG

    Ken Sprague Guest

    The java.lang.Class.isInstance method should work:

    if (c.isInstance(obj)) {
    ...
    }

    Ken
    Ken Sprague, Sep 22, 2003
    #6
  7. natG

    natG Guest

    Thank you all who replied.

    I should have mentioned that I need it to return true, if 'c' (the Class
    parameter) is passed an Interface, and obj (the first parameter) implements it.

    Therefore, I would assume that c.isInstance would do the trick.

    Correct?

    Thank you all,
    -nat
    natG, Sep 22, 2003
    #7
  8. "Skippy" <0spam.nl> wrote in message
    news:bknp8n$mv0$...
    > > > How can I check if an object is an instance of a class where the
    > > > class is represented in a Class variable?

    > >
    > > If you don't need inheritance
    > >
    > > if(obj.getClass().equals(c))

    >
    > if(obj.getClass().toString().equals(c.toString()))


    that won't do, as two different classes might have the same class name, even
    the same fully qualified one.

    > or maybe (don't know) :
    >
    > if(object.getClass() == c)


    that should work, but using equals seems more robust.

    -- j
    Jorn W Janneck, Sep 23, 2003
    #8
  9. natG

    Roedy Green Guest

    Roedy Green, Sep 23, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Kamoski
    Replies:
    3
    Views:
    15,616
    Jay B. Harlow [MVP - Outlook]
    Aug 9, 2003
  2. Nils

    static classvariable

    Nils, Nov 9, 2003, in forum: C++
    Replies:
    1
    Views:
    284
    Victor Bazarov
    Nov 9, 2003
  3. Phil Endecott

    Obj* ptr = new Obj(X)

    Phil Endecott, Jun 3, 2005, in forum: C++
    Replies:
    5
    Views:
    446
    Mark P
    Jun 3, 2005
  4. Replies:
    21
    Views:
    21,245
  5. Shalabh Chaturvedi
    Replies:
    2
    Views:
    423
    Mike C. Fletcher
    Feb 20, 2004
Loading...

Share This Page