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);
	}

Recommended Answers

All 6 Replies

If you give an object in stead of a String in a method, the object's toString() method is called.

In your example, this is happening in the line

System.out.println(b); // You pass an object instead of a String

.
What really is executed:

System.out.println(b.toString());

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);
	}

adding to what hiddepolen said: The toString() method, overrides the actual java toString() method, thats why its name cannot be changed... here is a bit on overridden methods:http://docs.oracle.com/javase/tutorial/java/IandI/override.html.

adding to what hiddepolen said: The toString() method, overrides the actual java toString() method, thats why its name cannot be changed... here is a bit on overridden methods:http://docs.oracle.com/javase/tutorial/java/IandI/override.html.

Aha !! Now, I know why. I never thought of method overriding. Thanks all for the replies.

If you give an object in stead of a String in a method, the object's toString() method is called.

In your example, this is happening in the line

System.out.println(b); // You pass an object instead of a String

.
What really is executed:

System.out.println(b.toString());

I know for sure. it is happening on this line.

System.out.println(b);

More about the fact that I was wondering why it is happening. it's because of method overriding. Thanks all again.

little remark about the original question, but I assume you figured it out already :)
it's not the constructor

<<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.

the constructor doesn't make a call to the toString method.

Thanks for the explanation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.