7

I am a newbie in java. Say, I have a class Individual. I want to print

Individual ind = new Individual();
System.out.println(ind);

Above code gives output like this:

Individual@1922221
  1. What is the significance of this?
  2. Is it some kind of unique id for that object?
  3. Can I customize this? I mean write a function of my own which will give output when I print?
  4. If so, how can I do this?
user
  • 5,335
  • 7
  • 47
  • 63

4 Answers4

10

If you want to print meaningful content of any object, you have to implement your own toString() method, which will override the parent(Object) class's toString() method. By default all the classes(Whatever you create) extends Object class.

Sample Code:

public class Individual {
    private String name;
    private String city;

    public String getName() {
        return name;
    }

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

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Name of Individual :").append(this.getName())
                .append("\nCity :").append(this.getCity());
        return builder.toString();
    }
    public static void main(String[] args) {
        Individual individual = new Individual();
        individual.setName("Crucified Soul");
        individual.setCity("City of Crucified Soul");
        System.out.println(individual);
    }
}

Output:

Name of Individual :Crucified Soul
City :City of Crucified Soul

If you have bigger class with many variables, you can use XStream to implement your toString() method. XStream will print your object meaningful in XML format. Even you can parse them back to equivalent object. Hope this would help you.

Ahamed
  • 39,245
  • 13
  • 40
  • 68
7

This is the result of default toString() method - the classname + hashcode. This can be override by overriding toString().

Some reference here: http://www.javapractices.com/topic/TopicAction.do?Id=55

Jan Zyka
  • 17,460
  • 16
  • 70
  • 118
2

Since it hasn't been explained yet, overriding the toString() method simply means that you create a toString() method of your own in your class. By putting your own version of toString() in your class, you make it so that java will use your toString() method rather than the default. Because the original toString() method returns a string, however, your toString() method must also return a string. Your Individual class would look something like this:

public class Individual{

    //any other code in the class

    public String toString(){
         return "your string";
    }

}

Then, when you called your System.out.print(ind); it would print out your string.

paul jerman
  • 601
  • 5
  • 13
1

I think you want to overwrite Individual toString. See http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#toString()

Kosta
  • 812
  • 1
  • 7
  • 13