10

I came across the code of guessgame. There is a code snippet where three player objects are initialized the following way:

public class guessgame{
    Player p1;
    Player p2;
    Player p3;
    public void startGame() {
        p1 = new Player();
        p2 = new Player();
        p3 = new Player();
    ...
    }
    ...
}

The same worked when I declared and initiated it the following way as well.

public class GuessGame {
    Player p1 = new Player();
    Player p2 = new Player();
    Player p3 = new Player();
    public void startGame(){
        ...
    }
    ...
}

Is there a difference between the two? In the first example, why was the three instance variables declared outside the startgame() method, and does it really matter internally?

13
  • 3
    The second version initialises the three players on instantiation of the GuessGame class, the first initialises them when startGame() is run. Commented Oct 23, 2015 at 13:50
  • 1
    @CaffeineToCode They're declared at the class level in both cases. Commented Oct 23, 2015 at 13:53
  • 1
    What does Linux have to do with this? Commented Oct 23, 2015 at 13:57
  • 2
    It can be useful to leave variables unitialialized at class level or initialize them as an empty Object, or something that indicates its unitialialized. This way, you can tell if a method has been executed yet. It really depends on the design of your code though. Commented Oct 23, 2015 at 14:00
  • 1
    The second example lacks a closing bracket on the startGame method. It's driving me a little bit crazy. Commented Oct 23, 2015 at 14:03

6 Answers 6

11

Is there a difference between the two?

Yes. The code in the first snippet will execute only in the context of startGame. If you do not call startGame, the objects would remain null. Every time you call startGame, the old objects, if any, would be discarded, and replaced with new ones.

The second snippet would execute once for each GuessGame object when you call any of its constructors. The code would be shared among all constructors.

In the first example, why was the 3 instance variables declared outside the startGame() method

The only way to declare an instance variable is to do it outside a method. Variables declared inside a method are locals.

Another difference between the first and the second way is that calling instance methods that access players becomes "legal" only after calling startGame in the first code snippet.

Sign up to request clarification or add additional context in comments.

Comments

7

If you initialize outside the method, then it is executed when the class is instantiated and memory is allocated for them. If you don't, it just gets a null (or default value for primitives) assigned to them.

If you never call startgame(), then you're delaying allocating it, and perhaps the user never wants to start the game. This is smart if the players take a lot of memory to build, and you don't want the user to wait for that to happen (memory allocation), and just run methods immediately (that don't use them).

If for some reason you have several methods that need to use the p1, p2, p3 variables, then coordination of who will initialize may be confusing, so you just might bite the bullet and initialize them upfront. If you know there will be 3 players, why bother the methods with it? Otherwise you'll have to do something like:

if (p1 == null){
    p1 = new Player();
}

2 Comments

The main advantage of initializing in startGame() is that you can always start a new game by calling startGame() and the players will be re-initialized. This matters if Player holds mutable data such as a score.
however, it's not mutually exclusive. say I call it restartGame() and initialize the objects again, should work fine too.
3

In the second method, you simply do both things at the same time - declare and initialize. The difference is, that you need to run startGame() method in the first example to initialize them.

Comments

3

On your first example, the objects are instantiated only when you call the method startGame(). Before the call to this method, p1, p2 and p3 are equal to null.

On your second example, you declare and instantiate them directly. So they are instantiated at the creation of the class.

Comments

2

1st : Initialization at method startGame()

guessgame gg = new guessgame() // Here p1 is null

2nd : at class level

GuessGame GG = new GuessGame() // OK p1 is initialized

Comments

1

p1, p2, p3 are not local variables, but class fields.

They are available to all class methods after the class instance was constructed.

In the first snippet, the values of the three fields are null until the method startGame() is invoked.

In the second snippet, the fields are initialized during instance construction.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.