• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Overriding the toString() method in Object class

August 21, 2007 //  by Krishna Srinivasan//  Leave a Comment

The toString() method in the Object class is used to display some information regarding any object. If any code needs some information of an object of a class, then it can get it by using this method. The toString() method of an object gets invoked automatically, when an object reference is passed in the System.out.println() method. The following code illustrates this,

also read:

  • hascode and equals method
  • A static utility for objects introduced in Java 7
  • Reading file asynchronously in Java
  • Virtual Extension Methods(or Defender Methods) in Java 8

ToStringMethodTest.java

public class ToStringMethodTest {
	private String companyName;
	private String companyAddress;
	public ToStringMethodTest(String companyName, String companyAddress) {
		this.companyName = companyName;
		this.companyAddress = companyAddress;
	}
	public static void main(String[] args) {
		ToStringMethodTest test = new ToStringMethodTest("ABC private Ltd","10, yy Street, CC Town");
		System.out.println(test);
	}
}

This output seems a bit weird. If we analyze it closely, we can find that the output is nothing but the Class name ToStringMethodTest and then the ‘@’ symbol is followed by 18d107f which is the hashcode of the object.

In case we would like to display some meaningful details of an object, we can override the toString() and thereby achieve it. Consider the following code,
ToStringMethodTest.java

public class ToStringMethodTest {
	private String companyName;
	private String companyAddress;
	public ToStringMethodTest(String companyName, String companyAddress) {
		this.companyName = companyName;
		this.companyAddress = companyAddress;
	}
	public static void main(String[] args) {
		ToStringMethodTest test = new ToStringMethodTest("ABC private Ltd","10, yy Street, CC Town");
		System.out.println(test);
	}

	public String toString() {
		return ("Company Name: " + companyName + "n" +
		"Company Address: " + companyAddress);
	}
}

The result of the above code is,

Company Name: ABC private Ltd
 Company Address: 10, yy Street, CC Town

What we have just now seen is just a sample of how a meaningful override of the toString() method would prove to be of great use in displaying an object’s information when we try printing an object using the System.out.println statement during debugging process.

also read:

  • hascode and equals method
  • A static utility for objects introduced in Java 7
  • Reading file asynchronously in Java
  • Virtual Extension Methods(or Defender Methods) in Java 8

Category: JavaTag: Core Java, Java

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Externalizable Interface in Java
Next Post: Threads Synchronization »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact