Threads and classes

Hello all.

How come when I make a class into a thread, I cannot use the class as an object anymore?

I’m calling it as

public Thread foo = new FooThread();

where FooThread has function bar() which printfs “FOOBAR”.

Using threads, how can I call inside my main loop?

Consult this tutorial for how to use threads in Java:

If FooThread extends thread, then you can still call, but you have to cast the pointer to the FooThread instance to be not a Thread but a FooThread.


Thread t = new FooThread();
//you can now start thread or do whatever

//you cannot, however do this:;
//Because not ever thread has bar() method. only FooThreads do.
//but this will work:
((FooThread) t).bar();

//Alternatively, you can create your pointer as a FooThread type as opposed to simply a thread
FooThread t = new FooThread();
//this way you can

Oh, wow, I can’t believe I missed that, XD