-1

Possible Duplicate:
What is the reason behind “non-static method cannot be referenced from a static context”?

I have the following method:

public void fillMachine(Game game)
{
  // Colours of balls are evenly spread between these colours,
  // in ascending order.
  Color [] colourGroupColours
    = new Color [] {Color.red, Color.orange, Color.yellow,
                  Color.green, Color.blue, Color.pink,
                  Color.magenta};
  // This happiness change will show up when the GUI is added.

  Color ballColour;
  int noOfBalls = game.getMachineSize();
  for (int count = 1; count <= noOfBalls; count++)
  {
    // The colour group is a number from 0
    // to the number of colour groups - 1.
    // For the nth ball, we take the fraction
    // (n - 1) divided by the number of balls
    // and multiply that by the number of groups.
    int colourGroup = (int) ((count - 1.0) / (double) noOfBalls
                           * (double) colourGroupColours.length);
    ballColour = colourGroupColours[colourGroup];
    game.machineAddBall(makeNewBall(count, ballColour));
  } // for

  } // fillMachine

In the main class I have fillMachine(game1);

I am getting the error: non-static method fillMachine(Game) cannot be referenced from a static context fillMachine(game1);

I'm not sure how to fix this though.

Community
  • 1
  • 1
AkshaiShah
  • 5,739
  • 11
  • 37
  • 45

3 Answers3

2

That's because you can't access non-static members from a static method. (unless you have an object to call the method on)

Your main method is static. That means it's not bound to a specific object of a class, but to the class itself. fillMachine is not static, meaning you can only call it on a concrete instance of the class. Which you don't have. You can either:

  • make the method static, if you're not using any non-static members inside of it.
  • create an object and call the method on the instance
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
2

You haven't really given us much context here, but basically your method is an instance method, so has to be called on an instance of the class. You can call it from a static method, but you'll need to have an instance to call it on. For example:

Foo foo = new Foo(); // We don't know what class this is in... you do.
foo.fillMachine(game1);

Of course you may already have an instance of the class which you've created elsewhere, and that may be the appropriate instance to call the method on.

Alternatively, you could make the method static if it doesn't need to refer to any instance variables.

It's important to understand the difference between static members (which are related to the class, not any particular instance of the class) and instance members (which are related to a specific instance).

See the Java Tutorial for more information.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

just change

public void fillMachine(Game game)

to

public static void fillMachine(Game game)
kajacx
  • 12,361
  • 5
  • 43
  • 70