0

I tried to call non-static drawScore() method from static calldrawScore(), but I got the error "cannot find symbol constructor Game" on line Game draw = new Game(); in calldrawScore(). When I pass with mouse over that line it says "GameScreen (Game) in Game cannot be applied to ()".

3

4 Answers 4

2

- Its rule of thumb that a static method can't access any non-static variable or methods.

- It is because static member belongs to the class where as non-static members belongs to the object, so as the static member tries to access a non-static member it won't be clear that which objects member is being accessed, so its prohibited in JAVA.

2
  • 2
    You can access a non static method from static method by creating an instance in the static method. Sep 10, 2013 at 9:45
  • @CodeEnthusiastic you are right about it, thats one way of defining a singleton principle, but as far as i know, there is no direct way of accessing a non-static member from a static member and was something i wanted the OP to know, but thanks for providing another angle to this answer.
    – nidhi
    Sep 10, 2013 at 10:42
0

Maybe if you can change "getScore(int x)" into:

public static int getScore(int x, GameScreen gs) {
        score = x;
        gs.drawScore();
        return score;
    }

and now you can call it in "GameScree" by

GameScreen.getScore(valueSome, yourObject);

Another way is to change all GameScreen into Singleton

0

Your class GameScreen constructor is taking Game class object as a parameter. You can either get the current Game instance and pass it as a argument or create a default constructor in your Gamescreen class.

0

To do GameScreen d = new GameScreen() you need to have 0-argument constructor for GameScreen. You don't have such constructor.

Anyway, your code looks preety bad, because you are creating new GameScreen in every calldrawScore()...

I think you need to read what's the difference between static and non-static methods. Then go back, design it and implement better.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.