Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to override toString method

I wrote a timer class. And I want to override its toString method. But when I call the toString method, it still returns the super implementation. (fully qualified name of the class)

Here is my timer class:

import android.os.Handler;
import android.widget.TextView;

public class Timer implements Comparable<Timer> {
    private Handler handler;
    private boolean paused;
    private TextView text;

    private int minutes;
    private int seconds;

    private final Runnable timerTask = new Runnable () {
        @Override
        public void run() {
            if (!paused) {
                seconds++;
                if (seconds >= 60) {
                    seconds = 0;
                    minutes++;
                }

                text.setText (toString ()); //Here I call the toString
                Timer.this.handler.postDelayed (this, 1000);
            }
        }
    };

    //Here is the toString method, anything wrong?
    @Override
    public String toString () {
        if (Integer.toString (seconds).length () == 1) {
            return minutes + ":0" + seconds;
        } else {
            return minutes + ":" + seconds;
        }
    }

    public void startTimer () {
        paused = false;
        handler.postDelayed (timerTask, 1000);
    }

    public void stopTimer () {
        paused = true;
    }

    public void resetTimer () {
        stopTimer ();
        minutes = 0;
        seconds = 0;
        text.setText (toString ()); //Here is another call
    }

    public Timer (TextView text) {
        this.text = text;
        handler = new Handler ();
    }

    @Override
    public int compareTo(Timer another) {
        int compareMinutes = ((Integer)minutes).compareTo (another.minutes);
        if (compareMinutes != 0) {
            return compareMinutes;
        }
        return ((Integer)seconds).compareTo (another.seconds);
    }
}

I can see that the text view's text is the fully qualified name of the Timer class. I even tried this.toString but it doesn't work either.

like image 941
Sweeper Avatar asked Aug 14 '15 12:08

Sweeper


People also ask

What happens if you dont override toString?

So, whenever you use or print a reference variable of type in which toString() method is not overrided, you will get an output like above. You will not get what the object actually has in it. There will be no information about state or properties of an object.

What happen if we override toString () method?

We can override the toString() method in our class to print proper output. For example, in the following code toString() is overridden to print the “Real + i Imag” form.

Why is toString not working?

Your Node class does not override the toString() method and falls back to use the Object. toString() method instead. Also I think it is a bit confusing that you add a value but return a Node instead of a value with get(). Update: to print the value of your Node add the following code to your Node class.


1 Answers

You're calling toString() from your anonymous inner class - the new Runnable() { ... }. That means you're calling toString() on your anonymous class instance, not on the Timer instance. I suspect you're getting a $1 in the output, showing that it's an anonymous inner class.

Try:

text.setText(Timer.this.toString());

... so that you call it on the enclosing Timer instance instead.

Here's a short but complete console app to demonstrate the difference:

class Test
{
    public Test() {
        Runnable r = new Runnable() {
            @Override public void run() {
                System.out.println(toString()); // toString on anonymous class
                System.out.println(Test.this.toString()); // toString on Test
            }
        };
        r.run();
    }

    public static void main(String[] args) {
        new Test();
    }

    @Override public String toString() {
        return "Test.toString()";
    }
}

Output:

Test$1@15db9742
Test.toString()
like image 137
Jon Skeet Avatar answered Oct 07 '22 01:10

Jon Skeet