Wednesday, 3 February 2016

Java Tutorial : Java Object Class(toString method)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=d6KJsZod0oM&list=UUhwKlOVR041tngjerWxVccw

Employee.java
public class Employee
{
    private String name;
    private int age;
    private int salary;

    public Employee(String name, int age, int salary)
    {
        super();
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public int getAge()
    {
        return age;
    }

    public void setAge(int age)
    {
        this.age = age;
    }

    public int getSalary()
    {
        return salary;
    }

    public void setSalary(int salary)
    {
        this.salary = salary;
    }

    /*
     * Returns a string representation of the object,
     * which is very useful for debugging.
     *
     * In general, the toString method returns a string that
     * "textually represents" this object.
     *
     * The result should be a concise but informative
     * representation that is easy for a person to read. It
     * is recommended that all subclasses override this
     * method.
     */

    @Override
    public String toString()
    {
        System.out.println("toString() method is called.");
        return "Employee [name=" + name + ", age=" + age
                + ", salary=" + salary + ", getClass()="
                + getClass() + ", hashCode()=" + hashCode()
                + ", toString()=" + super.toString() + "]";
    }
}
ObjectClassTest.java
public class ObjectClassTest
{

    public static void main(String[] args)
    {
        Employee peterObj = new Employee("Peter", 34, 50000);
        System.out.println(peterObj);

    }

}
Output
toString() method is called.
Employee [name=Peter, age=34, salary=50000, getClass()=class Employee, hashCode()=2114664380, toString()=Employee@7e0b37bc]
Click the below link to download the code:
https://sites.google.com/site/javaee4321/java/ObjectClassDemo_toString_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/ObjectClassDemo_toString_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/8fa82bcbf36a8ef675302adced6f7a0764627bd4/BasicJava/ObjectClassDemo_toString_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge