• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

instance of operator

 
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was reading following question from link
http://www.jchq.net/certkey/0501certkey.htm
>>>instanceof tests against a class name and not against an object reference for a class
did not understand it clearly.


import java.awt.*;

public class InOfComp {
public static void main(String argv[]){
}//End of main

public void mymethod(Component c){
if( c instanceof Button){
Button bc = (Button) c;
bc.setLabel("Hello");
}
else
if (c instanceof Label){
Label lc = (Label) c;
lc.setText("Hello");
}
}//End of mymethod
}

I could not see output on console. How to run and see the output to understand instanceof concept relating to child etc classes.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the first reason there's no output is because your main method is completely empty.

Mathew - after over 200 posts here, you should have learned how to UseCodeTags by now.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That code will work, but it is not object-oriented programming to test the type like that.
Do a search for "instanceof" and you will find other examples; I wrote about it on this forum about a week ago.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic