The toString Method - Java Object Oriented Design

Java examples for Object Oriented Design:To String

Description

The toString Method

Demo Code

public class TestToString
{
    public static void main(String[] args)
    {//from w  w  w.ja  v a2 s. c o  m
    Employee emp = new Employee("A", "B");
        System.out.println(emp.toString());

        int x = 50;
        String s = new Integer(x).toString();
        String s1 = "" + x;
        System.out.println(s);
        System.out.println(s1);
    }

}

class Employee
{
  private String lastName;
  private String firstName;

  public Employee(String lastName, String firstName)
  {
    this.lastName = lastName;
    this.firstName = firstName;
  }

  public String toString()
  {
    return this.getClass().getName() + "["
        + this.firstName + " "
        + this.lastName + "]";
  }

}

Related Tutorials