Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

public boolean equals(Object obj)

I've got double variables and I could of sworn that you do it this way.

public boolean equals(Object obj)
{
return length == obj.length && height == obj.height && width == obj.width;
}

error message
Error: cannot find symbol
symbol: variable length
location: variable obj of type java.lang.Object

Any and all help is appreciated! Stuck on this one method!
I am confused. What are you trying to return?
It must be a Boolean value that's returned (true/false).
Edit: == is used in conditional statements
Edit2: ignore me I'm a noob
[Image: zMpMcDB.png]
(Oct 6, 2015 01:45 PM)Raijin Wrote:  I am confused. What are you trying to return?
It must be a Boolean value that's returned (true/false).
Edit: == is used in conditional statements

It does return a boolean value and the usage of '==' is correct.

OP, your problem is that you're not casting the object. Your 'equals' method takes 'Object' as an argument, hence the program does not know exactly what is being passed as an argument because in java any class implicitly extends 'Object' (and therefore any object is an instance of 'Object'). Since the program doesn't know what kind of object it is, it can't access the fields 'length', 'height' and 'width' because it could be any other object that doesn't have those fields.
To fix your 'equals' method you have to cast the object. First off, if the object is not an instance of your class you can already return false. If it is an instance of your class, then you cast the object and perform the check. This can be achieved with this code:
Code
public boolean equals(Object obj) {
   if(obj instanceof ThisClass) {
      //Cast object so that the program knows what kind of object it is
      ThisClass castObject = (ThisClass)obj;
      //Now you can perform the check
      return length == castObject.length && height == castObject.height && width == castObject.width;
   } else {
      //Not an instance of ThisClass, can't be equal
      return false;
   }
}
The only addition Id make to TheCyberBricks post is that you shouldn't use the instanceof keyword when implementing the equals method because then classes that are subclass of the class will be true for that condition. Instead check the direct class of the objects and see if they're equal.
(Oct 6, 2015 04:55 PM)-Base- Wrote:  The only addition Id make to TheCyberBricks post is that you shouldn't use the instanceof keyword when implementing the equals method because then classes that are subclass of the class will be true for that condition. Instead check the direct class of the objects and see if they're equal.

You actually should use the instanceof in this situation. All subclasses will have those variables. The entire point of passing in an object instead of the actual class is so that you can include subclasses and not have to do a cast everytime you call that method.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by MankOct 4, 2020 07:53 PM 3 146 Oct 4, 2020 07:53 PM
Last Post: Mank
[C] by shadilayJun 30, 2019 03:52 PM 3 161 Jun 30, 2019 03:52 PM
Last Post: TGilb94
[C] by uid7683Mar 15, 2019 04:06 PM 1 143 Mar 15, 2019 04:06 PM
Last Post: YouthInAsia