How to print bean object in Java
To print any object in Java we have to convert it into string. The basic method we have to convert in to string is toString() method in java.lang.Object class. But this method by default prints the hexadecimal address value of the object. For example let us take the following Bean Class.
PersonBean Class
package com.javaindetail.bean;
public class PersonBean {
private String firstName;
private String lastName;
private String age;
private String address;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Let us print this object in main method
package com.javaindetail.main;
import com.javaindetail.bean.PersonBean;
public class PrintBean {</strong>
public static void main(String[] args) {
PersonBean bean = new PersonBean();
bean.setFirstName("Java");
bean.setLastName("In Detail");
bean.setAddress("India");
bean.setAge("21");
System.out.println(bean.toString());
}
}
The output will be the name of the class followed by hexadecimal address
com.javaindetail.bean.PersonBean@42e816
To print the bean object just override the toString() method of Object class. Check the following modified PersonBean Class
<pre>package com.javaindetail.bean;
public class PersonBean {
private String firstName;
private String lastName;
private String age;
private String address;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
//overriding toString() method of java.lang.Object class
@Override
public String toString(){
return "PersonBean[firstname="+firstName+", lastname="+lastName+", age="+age+", address="+address+"]";
}
}
output when we use the same main method to print the bean object.
PersonBean[firstname=Java, lastname=In Detail, age=21, address=India]
Apart from this technique of printing the bean object, we can use the Apache Commons Lang. In order to use Apache Commons Lang we have to add that jar and add the following code to the bean class.
public String toString() {
return new ToStringBuilder(this).
append("firstName", firstName).
append("lastName", lastName).
append("address", address).
append("age", age).toString();
}
or you can add these lines of code also instead of above
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
For Apache Commons Lang – Click Here
For Apache Commons Lang ToStringBuilder – Click Here
Enjoy Reading This Article?
Here are some more articles you might like to read next: