Converting Objects to Strings

   

As you learned in one of the earlier chapters, the Class Object implements a basic toString, which every class that inherits from Object will be able to use. The behavior in the Object class for the method just returns a String that is the name of the class, followed by the @ symbol, and a unsigned hexadecimal value of the hash code for the object.

You should most always override the toString method for your Java classes to provide a user -friendly String representation of the instance of the object. Listing 8.4 shows an example class that overrides the toString method to print out a more user-friendly representation of the instance.

Troubleshooting Tip

If you are not seeing the correct display string for one or more of your Java Classes, see "Incorrect String Representation," in the "Troubleshooting" section at the end of this chapter.


Listing 8.4 Customer.java ” An Example Class that Overrides the toString() Method
 public class Customer {   // Default Constructor   public Customer()   {     super();   }   // Private Instance Variables   private String id;   private String name;   private String city;   private String state;   private String zip;   // Public Accessors   public String getId()   {     return id;   }   public void setId( String custId )   {     id = custId;   }   public void setName( String newName )   {     name = newName;   }   public String getName()   {     return name;   }   public void setCity( String city )   {     this.city = city;   }   public String getCity()   {     return city;   }   public void setState( String state )   {     this.state = state;   }   public String getState()   {     return state;   }   public void setZip( String zip )   {     this.zip = zip;   }   public String getZip()   {     return zip;   }   // Override the default toString method to output something of your own   public String toString()   {     StringBuffer buf = new StringBuffer();     buf.append( "ID: " );     buf.append( getId() );     buf.append( " Name: " );     buf.append( getName() );     buf.append( " City: " );     buf.append( getCity() );     buf.append( " State: " );     buf.append( getState() );     buf.append( " Zip: " );     buf.append( getZip() );     return buf.toString();   } } 

If you had an instance of the Customer class, called the toString method on it, and sent it out to the console like this

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

the output would look this:

 ID: 12 Name: Bryan Daniel City: Atlanta State: California Zip: 11111 

This is much easier to read than the default, which might look like this:

 Customer@1e85 

You should always override the toString method for your classes if you need to. It will also help when debugging.

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net