aspose file tools
The moose likes Beginning Java and the fly likes How to overrule the toString method. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "How to overrule the toString method." Watch "How to overrule the toString method." New topic
Author

How to overrule the toString method.

Wilfried LAURENT
Ranch Hand

Joined: Jul 13, 2001
Posts: 269
Hi,
For debugging purpose, I want to display the references of objects.
However, some of this objects have overload the toString method. As a result I get :
"Value : 1
Date : 12/02/02"
instead of
"Toto@1323b930".
I know it is possible to get the references of the object but I can't remember how.
Any help would be appreciate.
W.
Mark Mokris
Ranch Hand

Joined: May 08, 2002
Posts: 61
I am guessing, but what about something like this?
String refNumber = ((Object)yourObject).toString();
I am guessing that if you cast any object to an (Object) you will force it to use the default toString() functionality.
I am just guessing.
Mark
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Well, you guessed wrong... The method is still invoked polymorphically, so that the type of the object will determine the actual method called, not the type of the reference (casting only affects reference types).


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9033
    
  10
You could do something like
if ( Object1 == Object2 ){ ...
to see whether the objects have the same reference or not.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Dave Landers
Ranch Hand

Joined: Jul 24, 2002
Posts: 401
Object's toString is:

But if hashCode is overriden, it won't help. In that case use System.identityHashCode:
Wilfried LAURENT
Ranch Hand

Joined: Jul 13, 2001
Posts: 269
It works! Thanks a lot Dave!
W.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: How to overrule the toString method.
 
Similar Threads
About String == String, please help.
synchronization issue
Eligible for Garbage Collection
first steps!!
Get Original Object