简体   繁体   中英

Java member initialization in constructors

I'm taking my first steps into a new programming language after 4 years in c++, and I've chosen Java. I am trying to make a simple tick tack toe game using classes, but I'm having a hard time understanding the differences between java and c++.

in one java class file I have:

public class Game
{
    Player p1, p2;
    public Game(String p1Name, String p2Name)
    {
        System.out.println(p1Name + " vs. " + p2Name);
    }
}

In a separate java class file I have:

public class Player
{
    private String name;
    public Player(String name_in)
    {
        name = name_in;
    }
}

I want to know, how to initialize Player p1, p2; in the Game class, since I don't want to give the Player class a default constructor. I'm sure I could just overload the constructors, like this:

 public class Game
{
    Player p1, p2;
    public Game(String p1Name, String p2Name)
    {
        p1 = new Player(p1Name);
        p2 = new Player(p2Name);
        System.out.println(p1Name + " vs. " + p2Name);
    }
}

public class Player
{
    private String name;
    public Player() { }       
    public Player(String name_in)
    {
        name = name_in;
    }
}

But, I'm wondering if there is a way to just initialize those objects without having to declare them and then initialize them. ie, just initialize them. If it were c++ I would just do this:

TL;DR: the code below is c++, how would I do a similar "one step member initialization" in Java.

class Game
{
    private:
    Player p1, p2;

    public:
    Game(std::string p1Name, std::string p2Name) : p1(p1Name), p2(p2Name) //<--One step member initialization
    {
        std::cout << p1Name << " vs. " << p2Name;
    }
}

class Player
{
    private:
    std::string name; 

    public:
    Player(String name_in) : name(name_in) { }  //<--Holy crap, another one.
}

There is no one step member initialization in Java. Your best bet is to construct the Player objects in Game 's constructor,

public class Game
{
    Player p1, p2;
    public Game(String p1Name, String p2Name)
    {
        p1 = new Player(p1Name);
        p2 = new Player(p2Name);
        System.out.println(p1Name + " vs. " + p2Name);
    }
}

or to receive the Player objects in Game 's constructor.

public class Game
{
    Player p1, p2;
    public Game(Player p1, Player p2)
    {
        this.p1 = p1; 
        this.p2 = p2; 
    }
}

In Java, there is no initializer like in C++, and there doesn't need to be one. Your original code was ok:

public class Game
{
    Player p1, p2;
    public Game(String p1Name, String p2Name)
    {
        p1 = new Player(p1Name);
        p2 = new Player(p2Name);
        System.out.println(p1Name + " vs. " + p2Name);
    }
}

From your question, you seem to be under the impression that with the above code you would need to add an (undesired) default constructor for class Player. Not so.

Remember that, unlike similar C++ declarations, the Java variables p1 and p2 are references to objects, not objects themselves. In the above code, p1 and p2 are first assigned to null and then, in the Game constructor, they are assigned to new objects. This may seem wasteful, but if it's run enough times the JIT will probably optimize away the unnecessary assignments to null, and just assign p1 and p2 directly to new objects.

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