• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

instanceOf(.) question

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey everyone,

I have a piece of code that creates two instances of the BattleField class called battleField1 and battleField2. Each battlefield is filled with a load of buttons. I have an ActionListener that performs an action when any button (on either battlefield) is clicked.

My problem is this. I want to create an if statement that determines whether or not the button clicked was from battleField1 or from battleField2. Here is my current code:

Basically, I need an aditional condition in the if loop that only executes the code if source is an instance of battleField1. I tried:

but I get a compiler error saying it cannot resolve symbol: method instanceOf (BattleField).

Am I using instanceOf() completely wrong? Hope you can help me,

Thanks lots buddies!

Smikey
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
instanceof is used to check whether an object is an instance of a specific class. To check if two variables reference the same object, you use the == operator, i.e.

 
bronco
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The previous response is more important, but her are a couple other notes...

1. The keyword is instanceof, not instanceOf.

2. instanceof is a java keyword, not a method - you don't need the parens around the thing you are checking (e.g. if (foo instanceof Bar)...)


Having said all that, I think you need to change your approach. The source that you get in your event is going to be the Button that was pushed, so checking to see if that object == frame.battleField1 isn't going to cut it.

I would suggest using a different listener for the button that needs to have different behavior. That way, you avoid the extra "if" all together. Subclassing might help if there is common code.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Careful with the use of == when comparing two Objects to check equality. It is possible that Object obj1 == Object obj2 returns false but the obj1 does equal obj2. The reason is that == when used to compare two objects is validating if the references point to the same memory location but does not validate that the objects are the exact same Object value. To check that two Objects are in fact the same object value you must use obj1.equals(obj2).

From your code snippet it appears you are trying to verify if the button pushed was a button from BattleField1 or BattleField2. Using the == will only validate if the source Reference points to the same point in memory as the form.battleField1 and not wether the Button pressed is the button in BattleField1. It is not safe to use == in this case as a new JButton object instance could have been created that represents the button in BattleField1 but will then fail the == check. It is safer and more correct to use source.equals(form.battleField1).

Mike
 
Mattias Arthursson
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Reterstorf:
Careful with the use of == when comparing two Objects to check equality.



In most cases, this is true. However, in some cases you really want to check whether the source actually references the same object, not if they are 'logically' equal (as in euqals() returns true).

The above example would be one of these cases, in my opinion. Since JButton doesn't have its own implementation of equals() it doesn't matter, but if it did you might be fooled using the equals() approach in this example.

Then again, the best solution of all in this case would of course be to do as suggested above; to have a different listener for each button.
 
Mike Smike
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey thanks guys.
Well I'll show you what I came up with:


and the field1 method is as follows:

I realise that this is probably a very inellegant solution... It does work though. Is my use of == ok in this case Mike?

Thanks loads guys!

Smikey
 
Dave Wood
bronco
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your use of == looks fine here.

There are times when you should use equals(), but I would not agree that this is a general rule that you should follow. Ideally, read up on the difference so that you really understand it and are able to use the right one for any given situation. You typically use equals() when the objects you are comparing are of a type that overrides the equals() method. This allows two different object instances to be "equal" if they represent the same data.

Here, you're comparing objects of a type that you created. Assuming you didn't override the equals() method in your class, it is almost certainly correct to use == here.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm with Dave's earlier post - an if test for which instance we're dealing with in every method is a strong signal to think about two listeners. Maybe the same class with some flags to control their behavior or maybe subtly different classes.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic