0

I tried to print a bean object as following.

CNSLResponseInfo csnlObject = new CNSLResponseInfo();
System.out.println(csnlObject .toString());

It gives output as below.

org.omo.model.CNSLResponseInfo@59d7305f

Any suggestions would be appreciated.

Thanks You

Rose18
  • 2,892
  • 8
  • 47
  • 98
  • You will need to override `toString()` of your class to explicitly specify what has to be printed. The default implementation of `toString()` returns / prints `ClassName@HexValueOfHashCodeOfInstance` – TheLostMind Sep 02 '15 at 07:55
  • You need to `@Override` default feature of `toString()` to print data in your format. – SaviNuclear Sep 02 '15 at 08:01

4 Answers4

1

Since you didn't ovveride toString(), it calls the default implementation you need to ovveride toString() and provide your own implementation.

Right now it's printing the default implementation of Object class toString() method, which is implemted in way to give the the unsigned hexadecimal representation of the hash code of the object.

To start with read What is the best standard style for a toString implementation?

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

You have to override toString method in your CNSLResponseInfo class.

wawek
  • 1,577
  • 1
  • 13
  • 23
1

You need to override toString() in CNSLResponseInfo class. Without it, the object class toString() is called, which is implemented to return FullyQualifiedClassName@Hashcode

Sategroup
  • 955
  • 13
  • 29
  • There is no guarantee that the second part will be *address* of the object – TheLostMind Sep 02 '15 at 07:59
  • @TheLostMind Can say `dynamic address` though ;) – Suresh Atta Sep 02 '15 at 08:01
  • 1
    @TheLostMind, sorry my bad, I meant the object's hashcode. Edited my comment – Sategroup Sep 02 '15 at 08:01
  • 1
    @sᴜʀᴇsʜᴀᴛᴛᴀ - Not necessarily. Check [this](http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#hashCode()). *As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)* :) – TheLostMind Sep 02 '15 at 08:03
  • @TheLostMind Agreed. Exactly my point too :) The internal address keep changes over time. – Suresh Atta Sep 02 '15 at 08:05
  • 1
    @sᴜʀᴇsʜᴀᴛᴛᴀ - Yes, but the hashCode remains the same :P – TheLostMind Sep 02 '15 at 08:06
0

Override the .toString() method of the class CNSLResponseInfo and then call System.out.println(csnlObject);

@Override
public String toString() {
    return prop1 + ", " + prop2 + ", " + prop3;
}
Shrinivas Shukla
  • 4,325
  • 2
  • 21
  • 33