2

I have this class:

public class PetAssembly extends Global
{
    public Socket socket;
    public ConnectionManager user;

    public PetAssembly(Socket socket, ConnectionManager user)
    {
        this.socket = socket;
        this.user = user;
    }

    public void initPet()
    {
        sendPacket(socket, "0|PET|I|0|0|1");
        sendPacket(socket, "0|PET|S|1000|1|0|8000|50000|50000|1000|1000|50000|50000|" + (((user.user.getSpeed() * 30) / 100) + user.user.getSpeed()) + "|testPet");
    }
}

I want to use it:

case "/pet":
      PetAssembly.this.initPet();
break;

But it gives me this error, how to fix it? I'm a beginner : No enclosing instance of the type PetAssembly is accessible in scope

2
  • 4
    new PetAssembly().initPet() ? Commented May 27, 2014 at 16:20
  • initPet() is an instance method, so you need to have an instance of the PetAssembly class in order to use the method. You can either make the initPet method static (which will require sendPacket method to be static also) or you could create an instance of your class in the case statement. Commented May 27, 2014 at 16:25

1 Answer 1

4

PetAssembly.initPet() is an instance method. You first need to construct an object of PetAssembly (an instance of that class), and then have a reference to that object before you can invoke a method on it.

PetAssembly pa = new PetAssembly(socket, user); 
// Creates a new PetAssembly object
// and stores a reference to that in the variable pa.
pa.initPet(); 
// Calls the initPet() method on the PetAssembly object referred to by the variable pa.
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.