This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes instance of operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "instance of operator" Watch "instance of operator" New topic

instance of operator

Mathew Lee
Ranch Hand

Joined: Jun 08, 2009
Posts: 238
I was reading following question from link
>>>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;
if (c instanceof Label){
Label lc = (Label) c;
}//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.
Matthew Brown

Joined: Apr 06, 2010
Posts: 4541

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.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 45292
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.
I agree. Here's the link:
subject: instance of operator
jQuery in Action, 3rd edition