Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Getting the properties of a class to construct a string representation

Tags:

Let's say I have a class like this (and also further assume that all the private variables:

public class Item {     private String _id = null;     private String _name = null;     private String _description = null;          ... } 

Now, if I want to build a toString() representation of this class, I would do something like this inside the Item class:

@Override public String toString() {     return (_id + " " + _name + " " + _description); } 

But what if I have say 15 private variables inside the class? Do I have to write the name of each and every variable like this?

Ideally, I would like to get over with the task by iterating through the list of private variables of this class and construct the string representation:

@Override public String toString() {     ArrayList<String> members = getClass().getMembers(); //Some method like this     String string = "";     for(...)         string += members[i] + " "; } 

Or perhaps a toJSON method, I would still need access to the names of these variables. Any suggestions?

like image 901
Legend Avatar asked Nov 23 '09 03:11

Legend


People also ask

Which method in Java is used to get the string representation of an object?

toString() method returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object.

How do we get a string representation of an object?

The toString() method returns the string representation of an object. A default implementation of this method is included in the Object class. This implementation returns a string containing the name of the object's class and its hash code.

Which method used to obtain string representation of a number?

We can use the toString() method to get the string representation of numbers as well and it can be useful if the string consists of numbers taken from different variables. In that case, the number can be converted to a string and concatenate to create a combined or formatted string.


2 Answers

You could do:

@Override public String toString() {   StringBuilder sb = new StringBuilder();   sb.append(getClass().getName());   sb.append(": ");   for (Field f : getClass().getDeclaredFields()) {     sb.append(f.getName());     sb.append("=");     sb.append(f.get(this));     sb.append(", ");   }   return sb.toString(); } 

Don't use string concatenation to construct an end result from 15 data members, particularly if the toString() will be called a lot. The memory fragmentation and overhead could be really high. Use StringBuilder for constructing large dynamic strings.

I usually get my IDE (IntelliJ) to simply generate toString() methods for me rather than using reflection for this.

Another interesting approach is to use the @ToString annotation from Project Lombok:

import lombok.ToString;  @ToString(excludes="id") public class ToStringExample {   private static final int STATIC_VAR = 10;   private String name;   private Shape shape = new Square(5, 10);   private String[] tags;   private int id;    @ToString(callSuper=true, includeFieldNames=true)   public static class Square extends Shape {     private final int width, height;      public Square(int width, int height) {       this.width = width;       this.height = height;     }   } } 

I find this much more preferable to, say, Jakarta Commons toString builders because this approach is far more configurable and it's also built at compile-time not run-time.

like image 172
cletus Avatar answered Oct 16 '22 12:10

cletus


Check this API org.apache.commons.lang.builder.ToStringBuilder, it provides multiple ways to create toString usinf reflection or without reflection. Take a look at other subclasses as well.

like image 36
Bhushan Bhangale Avatar answered Oct 16 '22 14:10

Bhushan Bhangale