简体   繁体   中英

Different ways to initialize variables in Java

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?

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.

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();
}

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.

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM