0

I have some code that demonstrates the use of threads by the use of the Runnable interface. I started with code off a website somewhere, and modified it to my liking. It works, but I don't understand part of it. I tried to strip the code down to the essence of what I am asking, but I may have taken too much out. The code I have in NetBeans works, so this is working code, unless I messed it up by taking the wrong thing out. But let me ask my question, and see if it can be answered regardless: The part I don't understand is this part:

public String toString()
       {
return "Thread " + Thread.currentThread().getName() + ": " + countDown;
       }

For the longest time, this just looked to me like a member variable whose name is dynamically set at runtime equal to the name of the current thread. But I have also read in more than one place that you cannot dynamically name variables in Java, so I guess that isn't what I'm looking at. Then, I realized that NetBeans wanted me to put @Override right before this code section, because something is being overridden. But I don't understand exactly what is being overridden. Am I overriding the constructor of some parent class? If so, what class?

Anyway, here is the code:

package countdown;

public class Counter implements Runnable
{
private int countDown = 5;
public String toString()
       {
return "Thread " + Thread.currentThread().getName() + ": " + countDown;
       }


public void run()
    {
   while(true) {
  System.out.println(this);
  if(--countDown == 0)
                      {
return;
                      }
                }
    }
}
package countdown;

public class Main 
{

public static void main(String[] args)
    {
for(int i = 1; i <= 5; i++)
  new Thread(new Counter(), "" + i).start();
    }

}
andcal
  • 1
  • 1
  • For clarity, as it is pertinent to this question: In Java, ALL classes will implicitly inherit from java.lang.Object. This is part of the language specification. So, as a lot of people have already mentioned, your IDE suggests @Override on toString() since it's one of the methods defined in the Object class. For more information on toString(), read the javadoc for java.lang.Object, http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#toString%28%29. – pap May 25 '11 at 06:35

2 Answers2

1

Every class implements toString( ) because it is defined by Object. However, the default implementation of toString( ) is seldom sufficient.

For most important classes that you create, you will want to override toString( ) and provide your own string representations. Fortunately, this is easy to do. The toString( ) method has this general form:

String toString( )   

The toString() method of an object gets invoked automatically, when an object reference is passed in the System.out.println() method.


@Override serves as documentation for the reader and a double check in the compiler. Use the @Override annotation to indicate that a method is overriding another in the base class.

For e.g

@Override
public String toString()
       {
return "Thread " + Thread.currentThread().getName() + ": " + countDown;
       }

See : When do you use Java's @Override annotation and why?

Community
  • 1
  • 1
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
0

Okay, what's up there is that you are overriding the Object.toString() method, which always returns some string representation of the named class. All it's doing is composing a string like

"Thread XXX: 5"

In Java, you can always override a parent classes (non-final) methods; the @Override tag is just helpful hinting for tools. But it's not setting the name of anything: it creates a new, anonymous string, and returns its reference.

If you called standard toString() on Thread, you'd get a string like "<#Thread 2348564>" or something equally useful; now, for this class, you get useful information.

Charlie Martin
  • 110,348
  • 25
  • 193
  • 263