toString() not a static method

J

Jean-Benoit MORLA

Hi,

I would like to print using System.err.println() the charasteristics of a thread.

My code includes a "main" method which starts three threads.

In the "run" method I try to insert the "toString()" call but I
get "Non static method toString() cannot be referenced from a static
context.
Is there a way around this?
Many thanks for your reply.
 
J

Joona I Palaste

Jean-Benoit MORLA said:
I would like to print using System.err.println() the charasteristics of a thread.
My code includes a "main" method which starts three threads.
In the "run" method I try to insert the "toString()" call but I
get "Non static method toString() cannot be referenced from a static
context.
Is there a way around this?
Many thanks for your reply.

Without seeing your code, it's hard to know what you're trying to do and
what is wrong. Do you have something like this in your code?

public static void main(String[] args) {
/* ... */
System.err.println(toString());
/* ... */
}

That's certainly not going to work, but from the above it sounds like
that's what you're trying to do. As I haven't seen your code, this is
only an assumption.

I assume you start your threads something like this:

Thread t = new Thread();
t.start();

Then you just need to write:

System.err.println(t.toString());

or even:

System.err.println(t);

As println() in class java.io.PrintWriter() automatically calls
toString() if given a parameter that is not a java.lang.String.
 
J

John C. Bollinger

Jean-Benoit MORLA said:
I would like to print using System.err.println() the charasteristics of a thread.

My code includes a "main" method which starts three threads.

In the "run" method I try to insert the "toString()" call but I
get "Non static method toString() cannot be referenced from a static
context.
Is there a way around this?

Invoke toString() on an object. The only way you should get a compiler
error like that is if you write code similar to the following:

class myClass {
static void myMethod() {
toString();
}
}

Because myMethod is static, there is no object on which to invoke
toString().

You wrote that your problem was in your run() method. That can only be
if you have made run() static, which will not work even if you could
make it compile. You must create an _instance_ of some class that
implements Runnable, construct a Thread to run it, and start that
Thread. To obtain a Thread object representing the thread running some
particular piece of code, you can use

Thread.currentThread()

To print the String representation of the currently running thread to
System.err, you would use

System.err.println(Thread.currentThread().toString());

or just

System.err.println(Thread.currentThread());

which will do the same thing.


John Bollinger
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top