Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > instanceof question

Reply
Thread Tools

instanceof question

 
 
Dotty
Guest
Posts: n/a
 
      02-16-2005
This question came up as a result of my reading
a news message about instanceof. Below is my code,
how does java know that "src" is a JButton?
----
public void actionPerformed(ActionEvent e)
{
Object src = e.getSource(); // get action source
if (src == DynamicPanel.dynamicResetButton)
{
// do stuff
return;
}
// other stuff
}


 
Reply With Quote
 
 
 
 
klynn47@comcast.net
Guest
Posts: n/a
 
      02-16-2005
src is a reference to the object that caused the event to occur.
Recall that when the == operator is used between object references, it
tests to see if the references point to the same object in memory.

So what's happening is you're asking is the Object that src refers to
the same object that dynamicResetButton refers to.

 
Reply With Quote
 
 
 
 
dar7yl
Guest
Posts: n/a
 
      02-16-2005

"Dotty" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> This question came up as a result of my reading
> a news message about instanceof. Below is my code,
> how does java know that "src" is a JButton?
> ----
> public void actionPerformed(ActionEvent e)
> {
> Object src = e.getSource(); // get action source
> if (src == DynamicPanel.dynamicResetButton)
> {
> // do stuff
> return;
> }
> // other stuff
> }


It doesn't. Java doesn't know anything about an object.
It is merely a language. No smarts there. The programmer
may know, and impart his wisdom in the code.

In this context, you don't care that src is a JButton.
You are just comparing the object "src" with the object
"DynamicPanel.dynamicResetButton". If they are equal,
ie, the same object, it will execute the context // do stuff...
Here, if you wish, you may cast src to JButton, but you
could test with instanceof to be absolutely sure. (If, for
instance, someone else had changed the type of
dynamicResetButton)

regards,
Dar7yl


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Another perplexing instanceof question -- can anyone please explain? System Administrator Javascript 1 03-11-2007 11:52 PM
instanceof NOT (always) bad? The instanceof myth. dmx_dawg@hotmail.com Java 21 07-20-2006 07:06 PM
instanceOf operator new bie question dbaplusplus@hotmail.com Java 14 05-02-2006 10:12 AM
Quick question on polymorphism and instanceof WillieLWZ Java 8 02-23-2006 06:56 AM
Re: Dumb instanceof question Paul Tomblin Java 35 07-27-2003 07:31 PM



Advertisments