I was just wondering why the constructor makes a call to the method toString() then I have realised that it's because of the method's name. And why is its name so unique? I changed the name but I don't get the same results. Anyone could possible explain this please? Thanks.
public class Bath {
private String // Initialising at point of definition
s1 = "Happy",
s2 = "Happy",
s3, s4;
private Soap castille;
private int i;
private float toy;
private String eid= "Eid";
public Bath(){
System.out.println("Inside Bath()");
s3 = "Joy";
toy = 3.04f;
castille = new Soap();
}
// Instance initialization
{ i = 47;}
public String toString(){
if(s4 == null) //delayed initialistion
s4= " Joy";
return
"s1 =" + s1 + "\n" +
"s2 =" + s2 + "\n" +
"s3 =" + s3 + "\n" +
"s4 =" + s4 + "\n" +
"i =" + i + "\n" +
"toy =" + toy + "\n" +
"castille =" + castille + "\n";
}
public static void main(String[] args){
Bath b = new Bath();
System.out.println(b);
}