This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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,
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.
Co-Author of <a href="http://www.oreilly.com/catalog/jswing2" target="_blank" rel="nofollow">Java Swing</a><br />Co-Creator of <a href="http://www.sun.com/training/catalog/courses/CX-310-055.xml" target="_blank" rel="nofollow">SCJP 5.0</a> and <a href="http://www.sun.com/training/certification/java/associate_beta.xml" target="_blank" rel="nofollow">SCJA</a> exams
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).
Joined: Jul 26, 2004
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.
Joined: Aug 25, 2004
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!
Joined: Aug 02, 2004
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.
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.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi