0

I am in the process of understanding various keywords. I got stuck with 'new' keyword. My question is that, why is there a need to create objects of the classes inside the Constructor. Would it make anydifference if you just did inside the other methods where you need them? What is the benefits of doing it this way?

    public class Game {

    private Board board;
    private GUI gui;
    private Die die;
    private ChanceDeck chanceDeck;
    private PlayerCollection players;
        
    private Game()
    {
        this.board = new Board();
        this.gui = new GUI(this.board.getGuiFields());
    
        this.die = new Die();
        this.chanceDeck = new ChanceDeck();
        this.players = new PlayerCollection();
    }
}
Henrik.A
  • 31
  • 5
  • You can't find a decent explanation for the use of `this.xxx` your code above because there is ***no need*** to use it in the situations shown above. It is superfluous and completely unnecessary. While it doesn't harm the code per se, it does make me wonder about the author of the code and why they feel it is necessary to use `this` in such an unnecessary way. – Hovercraft Full Of Eels Jan 19 '22 at 16:54
  • *"... but I could not quite find anything accurate in relation to this, otherwise it has just been hard to make it make sense to me."* -- and to me too – Hovercraft Full Of Eels Jan 19 '22 at 16:58

0 Answers0