0
public class UDP implements Runnable {

    private String host;
    private int port;
    private int delay;
    private int timeout;

    @Override
    public void handlePacket(String host, int port, int delay, int timeout) {
        UDP.host = host;
        UDP.port = port;
        UDP.delay = delay;
        UDP.timeout = timeout;
        Executors.newSingleThreadExecutor().execute(new UDP());
    }

The error im getting is non static field cannot be referenced to static context I have no idea what i should do, i know a little java but i am totally lost

3
  • You just permanently leaked a thread. Commented May 29, 2013 at 0:29
  • 2
    This question concerns a basic, fundamental aspect of object-oriented programming in general (not just Java). In my view, you ought to have looked to the online Java tutorial or any of the excellent Java sites for the answer to this question. Your first instinct should not have been to post this question to SO. Commented May 29, 2013 at 0:33
  • You should be using this, not new UDP(), as the argument to Executors.newSingleThreadExecutor().execute(). Commented May 29, 2013 at 0:35

1 Answer 1

6

Using the class name as a prefix indicates that you are attempting to use a static context.

To assign a value to an instance variable, don't prefix it with the class name, prefix it with this:

this.host = host;
...

Additionally, using this is only necessary because you named your parameters the same as your instance variables.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.