I have a program that needs a class file written to make it run and I cannot change the original program (a java file).
The java file that i cannot change instantiates a few versions of the class lets say something like this:
CrazyClass crazy1 = new CrazyClass();
CrazyClass crazy2 = new CrazyClass();
CrazyClass crazy3 = new CrazyClass();
//more code follows
It then sets variables in those newly instantiated classes something like this:
crazy1.var1 = 6;
crazy2.var1 = 7;
crazy3.var1 = 8;
//more code follows
Later the program actually calls those instantiated classes in a println statement like this:
System.out.println(crazy1);
and expects a message to pop out..
My question is how do I make this happen? Am I completely off base to think that classes that must be instantiated cannot have return values? I can only think that I need a method inside the class that returns the message and is automatically called, something like main but not static. Am I heading in the right direction here or totally off base? Please advise I have been reading all day and I fear I cannot see the forest for the trees at this point. Im sure its something simple.
Thank you in advance.
You’ll want to override
Object.toString().In the above print-statement, the
println()-method internally calls thetoString()-method on the given object. This method can be overwritten to return something meaningful. E.g: