Results 1 to 9 of 9
  1. #1
    nolsen01 is offline Member
    Join Date
    Jul 2009
    Posts
    18
    Rep Power
    0

    Default How can I find out what class this is?

    I have an EventObject which is the ancestor of KeyEvent.

    I'd like to find out if EventObject e is a KeyEvent.

    How can I do this?

  2. #2
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    Im not so sure about this..
    But maybe, Only Maybe.. you could do:
    boolean isAncestor = (KeyObject.getClass().getSuperclass() == KeyEvent)

    Idk. im not sure, but it would be my guess..

  3. #3
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Java Code:
    if (e instanceof KeyEvent) {
         KeyEvent e2 = (KeyEvent)e;
    }

  4. #4
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    Hum.. last time I checked instanceOf was broken, have they fixed that?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Addez View Post
    Hum.. last time I checked instanceOf was broken, have they fixed that?
    AFAIK instanceof was never broken; it checks whether or not the object's class is assignable from the other operand (a class). People sometimes don't expect the value to be true if a sub class of the class being tested is passed as the other argument.

    kind regards,

    Jos

  6. #6
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    Ohwell.
    When I tried to use it in one of my first codes for about half a year ago.
    Then it didnt work and I asked why and got the answer that it was broken :(

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Sure, if you tried instanceOf your code wouldn't compile. Java is case sensitive.

    db

  8. #8
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    yea I used the real instance of method even if I misspelled it in this post.

  9. #9
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    OK. "instanceof" is an operator, not a method. It is all lower case, no parentheses. Many consider using the operator poor practice, but that is only because some overuse the operator. For what you are doing, it is a valid approach and will work.

Similar Threads

  1. Replies: 6
    Last Post: 05-18-2010, 05:11 PM
  2. Could not find main class
    By Andyj in forum New To Java
    Replies: 2
    Last Post: 10-06-2009, 12:47 AM
  3. Can't find class
    By bozovilla in forum New To Java
    Replies: 2
    Last Post: 07-31-2008, 05:45 AM
  4. Could not find main class - JDK 1.6
    By Syranno in forum New To Java
    Replies: 1
    Last Post: 07-25-2008, 04:07 PM
  5. Replies: 0
    Last Post: 05-13-2008, 06:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •