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.
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.
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.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With