This discussion is archived
8 Replies Latest reply: Oct 19, 2006 7:07 AM by 807607 RSS

Why can't I do this? toString() question.

807607 Newbie
Currently Being Moderated
ArrayManipulater dataSet1 = new ArrayManipulater();

System.out.println(dataSet1.createRandomArray().toString());
It says cannot invoke string on the primitive type void.

I instead have to do this to make it work.
ArrayManipulater dataSet1 = new ArrayManipulater();

dataSet1.createRandomArray;

System.out.println(dataSet1.toString());
Also, I don't really get the difference between the built in toString functionality and a self-written toString method in a class. We were told to write a toString method that prints out an array but how does the compiler interpret our toString method versus the built-in toString method.
  • 1. Re: Why can't I do this? toString() question.
    807607 Newbie
    Currently Being Moderated
    It looks like the dataSet1.createRandomArray() method returns void (i.e., it doesn't return anything). Therefore, there's nothing on which to call toString(). If you want to chain method calls like your first example, modify createRandomArray() to return this.

    ~
  • 2. Re: Why can't I do this? toString() question.
    807607 Newbie
    Currently Being Moderated
    And then, you don't really need to explicitly invoke toString().
    println() will do that for you.
    If your "ArrayManipulater" class has a toString() method, the compiler will invoke it.
    If not, the default (Object's?) method will execute.
  • 3. Re: Why can't I do this? toString() question.
    807607 Newbie
    Currently Being Moderated
    call toString(). If you want to chain method calls
    like your first example, modify createRandomArray()
    to return this.
    @OP: ...and for an example where to use the said technique beautifully,
    just check out the design of java.lang.StringBuffer and use of its append(),
    insert() family of methods..
    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/StringBuffer.html
  • 4. Re: Why can't I do this? toString() question.
    807607 Newbie
    Currently Being Moderated
    Yeah, I figured it was because it was returning void but then I didn't get why I could do:

    dataSet1.createRandomArray();

    print dataSet1.toString();

    I mean, technically if it is returning void how can dataSet1 have a value?
  • 5. Re: Why can't I do this? toString() question.
    807607 Newbie
    Currently Being Moderated
    The built in toString method of the Object class (from which all java object are derived) returns a representation of the object that is not usefull for human consumption, in most cases. The String class is an exception n that is prints out the contents of the string itself. Whe you supplay a toString() method on your class, you choose what you what to print out..

    so a toString() method for a class that stores info about a bike might be like this.
    public class Bike {
    
         private String color;
         private int numSpeeds;
         private String brand;
         
         public Bike() {
              this("red", 10, "Schwinn");
              // TODO Auto-generated constructor stub
         }
         
         public Bike(String color, int numSpeeds, String brand) {
              super();
              // TODO Auto-generated constructor stub
              this.color = color;
              this.numSpeeds = numSpeeds;
              this.brand = brand;
         }
         
         public String getBrand() {
              return brand;
         }
         public void setBrand(String brand) {
              this.brand = brand;
         }
         public String getColor() {
              return color;
         }
         public void setColor(String color) {
              this.color = color;
         }
         public int getNumSpeeds() {
              return numSpeeds;
         }
         public void setNumSpeeds(int numSpeeds) {
              this.numSpeeds = numSpeeds;
         }
    
    
         public String toString()
         {
              StringBuffer sb = new StringBuffer();
              sb.append("Bike").append('\n');
              sb.append("Color: ").append(color).append('\n');
              sb.append("Number of Speeds: ").append(numSpeeds).append('\n');
              sb.append("Brand: ").append(brand).append('\n');
              
              return sb.toString();
              
         }
         
         
         
    }
    if you create an instance of Bike and call toString on it, you will get a String that looks like this:
    Bike
    Color: red
    Number of Speeds: 10
    Brand: Schwinn
    if you don't implement th toString method, the default Object toString method would be called instead, and you would get a string that looks like this
    Bike@1960f05
    so a toString method for an array would probably need to loop thru the array and print out each array element.
  • 6. Re: Why can't I do this? toString() question.
    807607 Newbie
    Currently Being Moderated
    > I mean, technically if it is returning void how can
    dataSet1 have a value?

    Think of methods as messages you send to an object. When you write "dataSet1.createRandomArray();", you're telling the object referred to by "dataSet1" to create a random array. When you write "dataSet1.toString();", you're telling the object referred to by "dataSet1" to give you a reference to a string representation of itself.

    ~
  • 7. Re: Why can't I do this? toString() question.
    807607 Newbie
    Currently Being Moderated
    the method createRandomArray is evidently changing the dataset object itself, instead of return the changed array

    to extend my Bike class, if I add this method
         public void changeColor()
         {
              color = "Blue";
         }
    the bike color is changed, but the change is made to the current Biek instance, it doesn't return a new Bike object.
    so if I try this
    Bike bike = new Bike();
    bike.changeColor();
    System.out.println(bike.toString());
    
    the output would be 
    
    Bike
    Color: Blue
    Number of Speeds: 10
    Brand: Schwinn
    
    but I could not do it this way
    
    System.out.println(bike.changeColor().toString());
    
    since the changeColor() method returns void.
  • 8. Re: Why can't I do this? toString() question.
    807607 Newbie
    Currently Being Moderated
    Wow, this was all very helpful info. It will take me a while to look through it all and adapt it to my program. Thanks a bunch.