Calling a specific instance's method in another class in Java

4.5k views Asked by At

Originally, this started as a nonstatic method being called from a static context error. I've since realized that I cannot reference the class itself, but a specific instance of that class. However, after giving my initialized object a specific name, I find that java will not acknowledge this specific instance, but instead refer to is as an unknown variable. When attempting to compile the code, I receive the error " cannot find symbol - variable player". I believe the code isn't recognizing the particular instance of the class as an already declared variable.

Here is the class being initialized

Link player = new Link(); 
addObject(player, 300, 176);

and here is the method trying to reference this specific instance's method:

int linkX = player.getX();

The second bit of code is in a method belonging to a class by the name of ChuChu. ChuChu and Link are subclasses of the same superclass. Any help would be greatly appreciated!

World class:

  public Link player;

/**
 * Constructor for objects of class ThroneRoom.
 * 
 */
public ThroneRoom()
{    
    // Create a new world with 600x562 cells with a cell size of 1x1 pixels.
    super(600, 562, 1);
    this.player = new Link(); 
    prepare();
}

/**
 * Prepare the world for the start of the program. That is: create the initial
 * objects and add them to the world.
 */
public void prepare() //R1
{
    addObject(player, 300, 176);
    addObject(new ChuChu(), 45, 267);
    addObject (new ChuChu(), 558, 267);
    addObject ( new ChuChu(), 45, 373);

}

Method for ChuChu in full

/**
 * Crawl Toward - Makes ChuChus crawl toward Link
*/
public void crawlToward ()
{
    int random = (int) Math.random() * 5;
    int linkX = player.getX();
    int linkY = player.getY();
    if(getX() >linkX)
    {
        setLocation(getX() - random, getY());
    }
    else
    {
        setLocation(getX()+random, getY());
    }

    if(getX() > linkY )
    {
        setLocation(getX(), getY()-random);
    }
    else
    {
        setLocation(getX(), getY()+random);
    }

}

I am using an IDE called Greenfoot. My problem seems to be that I am given two classes, World and Actor, and they cannot inherit from each other. From the World class, I instance objects into the visual world, and in Actor I create the objects and their methods.

1

There are 1 answers

3
Jason Sperske On

Based on your clarifying comment this might help, Java is block scoped so something like this wouldn't work:

if(true) {
  Link player = new Link(); 
}
//player is outside of the block it was declared in
int linkX = player.getX();

In your case y need to declare player outside of private void prepare() (at the class level would make the most sense in this case). you are free to instantiate it inside a block (or method in your case). Here is a quick attempt at doing this with your class:

public class World
{
  Link player;

  public ThroneRoom()
  {    
    // Create a new world with 600x562 cells with a cell size of 1x1 pixels.
    super(600, 562, 1); 

    prepare();
  }

  /**
   * Prepare the world for the start of the program. That is: create the initial
   * objects and add them to the world.
   */
  private void prepare()
  {
    this.player = new Link();
    addObject(player, 300, 176);
    addObject(new ChuChu(), 45, 267);
    addObject (new ChuChu(), 558, 267);
    addObject ( new ChuChu(), 45, 373);

  }
  public void crawlToward ()
  {
    int random = (int) Math.random() * 5;
    int linkX = this.player.getX();
    int linkY = this.player.getY();
    if(getX() >linkX)
    {
      setLocation(getX() - random, getY());
    }
    else
    {
      setLocation(getX()+random, getY());
    }

    if(getX() > linkY )
    {
      setLocation(getX(), getY()-random);
    }
    else
    {
      setLocation(getX(), getY()+random);
    }
  }
}