• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Basic - Using Object's getClass() method

 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats was indeed a good explanation!
I just like the way you explain, Rob!
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After seeing how Rob solve query.
I always feels why not i should also try practising API more,more,more and more
 
Rob Spoor
Sheriff
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic