0

Am I going about this the correct way? I generated the toString().

@Override
public String toString()
{
return "Auto [exampleOne=" + exampleOne + ", exampleTwo=" + exampleTwo + ", exampleThree=" + exampleThree ", getexampleOne()=" + getexampleOne() + ", getexampleTwo()=" + getexampleTwo() + ", getexampleThree()=" + getexampleThree() + ", toString()=" + super.toString() + "]";
}
user9424843
  • 33
  • 1
  • 5

2 Answers2

3

Yes, this overrides the toString() method, which will be called when implicitly casting an object of this class to a String. What will be displayed depends on the String your function is returning.

Note that @Override is already helping you figure this out. It tells the compiler "hey, I want to override a function, please check, if this is actually being done" (see here).

3ch0
  • 173
  • 1
  • 7
0

Java Specification does not use toString() as a logic of smth. and does not strict it. So the developer is free to implement this method on his own. I think there are a couple of recommendations:

  • it should be simple;
  • it should be easy to calculate;
  • it should not throw an exception;
  • IDE is used it to should class' object in the debugger, so it's better to print the most important values here in one simple line.

I do not recommend to use this method as user's output string or smth. like that. You should create a separate method for the formatted output string.

Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35