Hey just a question to prove me right or wrong. I am wondering what the 'this' in the following code refers to in terms of my applet.

mainThread = new Thread(this);

I believe that it refers to the run method which defines the thread. I was told I was wrong but given no answer so could someone enlighten me as to what it does?

Thanks a lot.

Lj

Recommended Answers

All 3 Replies

In Java "this" is always the current object - the one whose context the current method is executing in. The Thread constructor takes an instance of Runnable as its parameter, so "this" must be an object whose class implements the Runnable interface (which consists of a single run method). When the thread is started it will call the run method for the "this" object.

Thanks very much. Thats what I said only not in so many words.:-)

Java programmers are very pedantic - they have to be. You said
"I am wondering what the 'this' in the following code refers to ... I believe that it refers to the run method".
So technically that's false, even if you did have the right idea.
Anyway, mark this thread solved?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.