-2

I have 2 objects:

  1. ObjectA
  2. ObjectB

When I turn ObjectA to a string (ObjectA.toString()) it looks like this:

projectA.test.Object@e6db18d

Now with ObjectB when I turn it to a string (ObjectB.toString()) it looks like this:

ObjectB {color=StringProperty [value: blue], car=StringProperty [value: bmw]}

Now my question is why does one present a a string of numbers and letters while the other one presents me with a list? How can I do it that all my objects are presented like lists similar to ObjectB as this would be easier to read?

  • 1
    I believe ObjectB and ObjectA are not objected of the same class. Override toString in class of ObjectA – Amit Bera Jul 31 '18 at 17:31
  • 2
    Because `ObjectA` class has not overriden toString method and `ObjectB` class have – Ashishkumar Singh Jul 31 '18 at 17:31
  • 1
    Your problem is interesting but please provide a Example to reproduce the behaviour... ObjectB probably override toString to return something and ObjectA defaults to the implementation – Marcos Vasconcelos Jul 31 '18 at 17:32
  • It was a question given to me by a friend . Was wondering about it but yes, now that you mention it, I am sure that one had toString() overriden and presented it like in the example above. Thank you all. – Steven Anderson Jul 31 '18 at 17:40

1 Answers1

-1

The string value of an object is obtained through the toString() method. Therefore, to change the string representation of an object, the toString() method must be overridden in the class:

public class SomeClass {

    @Override
    public String toString() {
        return "Custom string";
    }

}

The string representation of ObjectA is the default string representation of any object, where the name of the class of the object is printed, followed by @, and finally, the hash code of the object. The default implementation is:

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
Justin Albano
  • 3,809
  • 2
  • 24
  • 51