Results 1 to 4 of 4
  1. #1
    darek9576 is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default Use of keyword instanceof

    Can anyone outline the use of the keyword "instanceof".
    I mean, when do we use it etc..

    Couple of examples would make it clearer i guess..

  2. #2
    uniflare is offline Member
    Join Date
    Mar 2010
    Posts
    6
    Rep Power
    0

    Default

    wellif its anything like php, its a conditional operator such as == etc.

    eg,

    if(SomeSpecificClass instaceof SomeSpecificClassorInterface){
    // You knw this class either extends somespecificclass or implements somespecificinterface.
    }

    Mainly used to make sure modular code aheres to the overall program design. (OOP, MVC etc)

  3. #3
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    it is quite useful if we use objects. the most remarkable example is swing mouse clicking listener like a



    Java Code:
    public void mouseClicked(MouseEvent e)
    {
      if(e.getComponent() instanceof JButton){return;}
    }
    as a rule it is much useful if you cannot predict what object is to be chosen by user etc... something like an object ensurance :cool:

  4. #4
    Niekfct is offline Member
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Default

    Say you have a method, needing a parameter of an instance of the "MyObject" object.
    Then, we have 2 objects, extending the MyObject: YourObject and OurObject. If you want to do different things when receiving a MyObject you can do this as follows:

    Java Code:
    public void myMethod(MyObject mo)
    {
        if(mo instanceof YourObject){
           //do some stuff.
        }else if(mo instanceof OurObject){
           //do some other stuff.
        }else{
           //mo is an instance of MyObject, no extension.
        }
    }

Similar Threads

  1. Keyword : volatile
    By peiceonly in forum Advanced Java
    Replies: 13
    Last Post: 11-17-2010, 01:47 PM
  2. this keyword
    By coltragon in forum New To Java
    Replies: 10
    Last Post: 03-01-2010, 10:20 AM
  3. super instanceof Class?
    By mikeiz404 in forum New To Java
    Replies: 11
    Last Post: 01-23-2009, 08:23 PM
  4. using instanceof to get Object type and parent type?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 11-06-2008, 07:24 PM
  5. Use of this keyword
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-18-2007, 08:32 PM

Posting Permissions

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