Pengiuns Pengiuns - 1 year ago 90
Java Question

Unable to access method of thread class?

Newb here so apologies

public class Threads implements Runnable{
public String threadName;
private volatile boolean running = true;

public Threads( String name){
threadName = name;

public void setoff(){
running = false; //this.running = false;

public void seton(){
running = true; //this.running = true;

public void run() {
if(this.running ==false){
System.out.println("Thread " + threadName + " exiting.");


--end class

public class MainFrame extends javax.swing.JFrame {
Thread R1 = null;
public MainFrame() {

private void jbtnstartActionPerformed(java.awt.event.ActionEvent evt) {
R1 = new Thread(new Threads("Thread 1"));

I'm unable to call any functions of R1 from my main class? (seton(), setoff() etc etc)

Any help would be greatly appreciated.

Answer Source

You can't call any methods of your Threads class, because you don't have a reference to the object of that type.

Change your code to:

Threads R1 = new Threads("Thread 1");
Thread thread = new Thread(R1);


Also, if(this.running ==false) should be written if (! this.running).

Or better yet, that line should be removed. The only way for that line to be executed, is for the preceding while loop to exit, which only happens when this.running is false, so the test in the if statement is redundant.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download