0

I'm doing my assignment, and have run into some errors. In one class I have this method:

public class Class1{
    public static boolean winningRecord(){
        final int WINNING_RECORD;
        return Class2.getPoints() > WINNING_RECORD;
    }
}

class Class2{
    int wins = 0;
    public int getPoints(){
        return wins; //More to it but to keep it simple I'll leave that out
    }
}

Now I'm getting the error "Non-Static method cannot be referenced from a static context...", so I made getPoints() a static method, made the variables static as well, and it works. But in another method for printing out Objects it doesn't work (I believe it's because of the static keyword).

So my question after all this is there a way to call a method without creating an instance of the second Class? This is the general idea code that I have, it should give you an understanding of what is going on, if not I'll add more to it.

Dan W
  • 5,718
  • 4
  • 33
  • 44
Matthew Brzezinski
  • 1,685
  • 4
  • 29
  • 53
  • `getPoints` can't be static unless `wins` is static. And if `wins` is static then there will only ever be one copy of `wins`, shared by the entire application. You need to decide how many copies of `wins` you need. (And "singleton's aside, it's generally silly to have a class just for storing one static data value.) – Hot Licks Jan 28 '13 at 17:12
  • You need to elaborate on "But in another method for printing out Objects it doesn't work (I believe it's because of the static keyword)." making getPoints() static should not in itself be a problem. – djna Jan 28 '13 at 17:14
  • I have another method which prints out Objects, however when it goes to print out the variables values it just outputs the same thing for each Objects. – Matthew Brzezinski Jan 28 '13 at 17:16
  • What you are asking is purely related to understanding of OOPS basics. – CuriousMind Jan 28 '13 at 17:19
  • 1
    Class2 has an instance variable named wins. `getPoints()` returns that instance variable, if you have no instance of Class2 then it does not make sense that calling the method statically would ever work, hence the reason you get the error. Like @JonSkeet said, get familiar with what `static` means. It will pay in large dividends. – Chris Wagner Jan 28 '13 at 17:19
  • Does your real code supply a value for WINNING_RECORD? It seems like it shouldn't compile without that. `final int WINNING_RECORD = 232;` – Mel Nicholson Jan 28 '13 at 18:17

3 Answers3

22

A key concept in java is the idea of instantiation. A class definition has all the rules for one type of object. Each instance of the object will follow the same rules. For example, if I define

class Ball {
  public void bounce() { 
    // lots of code here
  }
}

Then the system has code for things called Balls which can bounce

If I want two balls...

public class Main {
  public static void main(String args[]) {
    Ball beachBall = new Ball();
    Ball soccerBall = new Ball();

and I can bounce them all I want

    beachBall.bounce();
    beachBall.bounce();
    soccerBall.bounce();
    beachBall.bounce();

but I cannot say

    Ball.bounce();

because that leaves my system with the question "Which ball?"

static methods and variables are not associated with specific instances.

For example:

class Werewolf {
  static boolean fullMoon = false;

  static void setFullMoon(boolean isVisible) {
    fullMoon = isVisible;
    // code code code
  }

  void eatPerson(Person p) {
    // code code code
  }
}

The static method applied to all werewolves so it is called on the class:

Werewolf.fullMoon(true);

The non-static (instance) method applied to a particular werewolf so it is called on an instance.

jeff.eatPerson(chuck);

Jeff is the werewolf. Chuck is the person he eats. Try to ignore the cruel comments. We all started somewhere.

Mel Nicholson
  • 3,225
  • 14
  • 24
0

In your code, winningRecord() is specific to the class Class1 itself due to static keywork.

whereas getPoints() in Class2 is specific to an instance of class Class2

It is not possible to access a non static method from a static context.

sr01853
  • 6,043
  • 1
  • 19
  • 39
0

getPoints can't be static unless wins is static. And if wins is static then there will only ever be one copy of wins, shared by the entire application. You need to decide how many copies of wins you need. (And "singleton's aside, it's generally silly to have a class just for storing one