toString method for varargs contructor

Posted by owca on Stack Overflow See other posts from Stack Overflow or by owca
Published on 2010-03-16T23:24:03Z Indexed on 2010/03/16 23:31 UTC
Read the original article Hit count: 389

Filed under:
|
|
|

I have a varargs contructor like this :

public class Sentence {

   public String[] str;

      public Sentence(Object... text){

        StringBuilder sb = new StringBuilder();
        for (Object o : text) {
          sb.append(o.toString())
          .append(" ");
        }
        System.out.println(sb.toString());

     }

 }

Contructor can get various types of data (ints, strings, and Sentence objects as well). How to do a proper toString method for such class ?

© Stack Overflow or respective owner

Related posts about java

Related posts about constructor