简体   繁体   中英

Cant call a void method in a class in java

I'm trying to call a method in another class, however, I'm getting an error. I initialize the class to a variable, but I do not get the option to call that method. not even the variable shows up in the auto-complete when I start typing.

this is the class I'm calling:

public class aMessageBuffer {


private Collection<Message> messageBuffer = new ArrayList<Message>();
private Collection<Integer> responseBuffer = new ArrayList<Integer>();

private boolean messageBufferFull = false;
private boolean responseBufferFull =  false;


//Called by aProducerTask
public void sendMsg(String op, int val){
    //place message in messageBuffer
    Message msg = new Message();
    msg.setValues(op,val);
    messageBuffer.add(msg);

    messageBufferFull = true;

    //todo: notify
    while (responseBufferFull == false){
        //wait...
    }
    //remove response from responseBuffer
    responseBuffer.clear();

    responseBufferFull = false;

    //todo: Out Response
}
}

and this is where I'm calling it from:

public class aProducerTask extends Thread {
    //TODO: Send Messages
    private aMessageBuffer msgbuf = new aMessageBuffer();
    msgbuf.sendMsg("add",3);

    //TODO: Print Results
}

I'm getting the error in the msgbuf.sendMsg("add",3); it is not allowing me to call the method with that variable

This is my Main:

public class Main{
    public static void main(String args[]){
        new aConsumerTask().start();
        new aProducerTask().start();
    }

    public void run() {


    }
    }

You should define run method in your class aProducerTask.

public class aProducerTask extends Thread {
    //TODO: Send Messages
    private aMessageBuffer msgbuf = new aMessageBuffer();

    public void run() {
        msgbuf.send("add",3);
    }

    //TODO: Print Results
}
  1. Don't call non-assignment methods naked in the class and outside of any method, constructor or similar block.
  2. Here you'll want to call the non-assignment method in the run method.
  3. Don't extend Thread, implement Runnable and give it a run method which will hold your method call. Then create a new Thread when needed, pass in the Runnable and call start() on the Thread.
  4. You will want to learn and use Java naming conventions . Variable names should all begin with a lower letter while class names with an upper case letter. Learning this and following this will allow us to better understand your code, and would allow you to better understand the code of others.
  5. In the future if you have similar questions about a compilation error, post the full error message with your question, and please proof-read your code for correctness. 90% of our comments to questions are about clarification and correction.

So not:

public class aProducerTask extends Thread {
    //TODO: Send Messages
    private aMessageBuffer msgbuf = new aMessageBuffer();
    msgbuf.sendMsg("add",3);

    //TODO: Print Results
}

but rather this:

// improve class name and have it implement Runnable    
public class AProducerTask implements Runnable {
    private aMessageBuffer msgbuf = new aMessageBuffer();

    @Override // don't forget this!
    public void run() {
        msgbuf.sendMsg("add",3);
    }
}    

Then to use:

public new Thread(new AProducerTask()).start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM