0

I have a Game class and a TestGame class, I'm trying to call a method from Game to TestGame.

TestGame.java

public class TestGame {

    Players ceri = new Players("Ceri", 1);
    Players harry = new Players("Harry", 1);
    Players lewis = new Players("Lewis", 1);
    Players kwok = new Players("Kwok", 1);
    Players james = new Players("James", 1);
    Players matthew = new Players("Matthew", 1);


    Game league = new Game("League Table");

    league.addplayer(ceri);

    public static void main(String[] args) {

    }
}

and Game.java

public class Game {

    private String name;

    public Game(String name){
        this.name = name;
    }

    public void addPlayer(Players obj){

    }
}

I get red lines underneath league.addPlayer for some reason.

5
  • 1
    You don't even have a constructor with two arguments.
    – vikiiii
    Jul 23, 2014 at 14:43
  • 1
    "I'm probably going to get down voted for this.." then fix your question so you won't. It sounds like you already know what's wrong with it, which is a good start.
    – djechlin
    Jul 23, 2014 at 14:45
  • league.addplayer(ceri); needs to be inside an appropriate method of some sort - it currently isn't so it's a compile time error
    – JonK
    Jul 23, 2014 at 14:45
  • make the objects in the main method. Jul 23, 2014 at 14:52
  • Further to the answers that have been posted: You've got a method called addPlayer defined in Game, but you're trying to call addplayer. Capitalisation matters, those two are not the same. You'll need to capitalise that p as well as address the other problems with that line.
    – JonK
    Jul 23, 2014 at 15:01

4 Answers 4

5

league.addplayer(ceri); is code, not initializer.

You need to put it inside a method or inside an initalizer block.

In this case put it inside your main method.

5
  • In this specific case, it won't work in a static initialization block, but well in an instance initialization block. Jul 23, 2014 at 14:43
  • 1
    The Players and Game variables should also be static, as you would be referencing them from a static context Jul 23, 2014 at 14:45
  • @MatiCicero Better to create an instance of the class and invoke the methods on it rather than just making everything static unnecessarily.
    – JonK
    Jul 23, 2014 at 14:47
  • Where is that written in the question? I see no indication of it. The main method? As far as I can see, it's empty, so it's not a good clue. Jul 23, 2014 at 14:48
  • @JonK I agree it would be the best option, I'm just giving extra tips if the OP's gonna follow @Tim B advice and move the addPlayer method to the main Jul 23, 2014 at 14:50
2

You'd have to move all of these lines to the main() method:

Players ceri = new Players("Ceri", 1);
Game league = new Game("League Table");
league.addplayer(ceri);

If you move only the last one, the league and ceri variables won't be accessible, as the main method is a static method and these variables would be instance variables.

0

All of your Players (and Game) are declared inside the class body. This makes them global variables with default level access. Something to note in addition to Tim B's answer.

0

You should put your code inside the main block.

public static void main(String[] args) 
{
    league.addplayer(ceri);
}
1
  • Note that this will give you a compiler error saying "Cannot reference non-static variable 'league' from a static context" - or something to that effect.
    – JonK
    Jul 23, 2014 at 14:48

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.