aspose file tools*
The moose likes Beginning Java and the fly likes Basic - Using Object's getClass() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Basic - Using Object Watch "Basic - Using Object New topic
Author

Basic - Using Object's getClass() method

André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
I have read the JavaDoc however I did not understand well how would I use Object's getClass method... Look at my tried:



Does anybody could help me understand that method?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

Let's walk through it line by line (well, almost).
Any object must be equal to itself. This is just a shortcut that will prevent checking all fields if you're comparing an object to itself (as it's fields will be equal of course). This check can be skipped but is mostly included for (slightly) better speed.
Any object will never be equal to null. Pretty clear.
Now this is a bit interesting. Most people use the instanceof operator to determine if an object is an instance of the current class (e.g. obj instanceof Date). However, that may break the symmetry rule for Object.equals: if x.equals(y) then y.equals(x). An example using instanceof will show how this can be broken:
Now assume we have a Point p and a ColorPoint cp. Both have x == 0 and y == 0. cp has a red color. Now p.equals(cp) will return true -- cp is also a Point and has the same x and y values. However, cp.equals(p) will return false. Sure, the x and y values are the same, but p is not a ColorPoint.

This is where the getClass() comparison comes in place. Even if you call getClass() inside Point.equals, it will return the Class object for ColorPoint if called in cp.equals(p).
In p.equals(cp) the check will be if Point.class == ColorPoint.class, which returns false. In cp.equals(p) the check will be if ColorPoint.class == Point.class, again false. So now p.equals(cp) and cp.equals(p) will both return false.

This is simple casting to the class and checking one field, which is allowed to be null. If getIdioma() returns null for both or they are equal the method will now return true; otherwise it will return false.

I have one suggestion though: either use the field (idIdioma) or the method (getIdIIdioma()), but not both. If a sub class overrides this method to return something else you have two different checks where there should be one.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Thats was indeed a good explanation!
I just like the way you explain, Rob!


OCPJP 6
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

After seeing how Rob solve query.
I always feels why not i should also try practising API more,more,more and more


SCJP6.0,My blog Ranchers from Delhi
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

Vinoth Kumar Kannan wrote:Thats was indeed a good explanation!
I just like the way you explain, Rob!

Thank you

Shanky Sohar wrote:After seeing how Rob solve query.
I always feels why not i should also try practising API more,more,more and more

And that's exactly how I got at my current level - reading and practicing. And more practicing. And then, for some diversity, practicing a bit more
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Basic - Using Object's getClass() method