Declaration of attributes in a Java class [closed]

1

Why were the declarations of these variables made like this?

public class Jogo {

    private Tabuleiro tabuleiro; <<-
    private int rodada=1, vez=1;
    private Jogador jogador1; <<-
    private Jogador jogador2; <<-
    public Scanner entrada = new Scanner(System.in);

    ...
}
    
asked by anonymous 04.04.2018 / 10:09

1 answer

0

Well, let's look at a snippet of code:

private Tabuleiro tabuleiro
  • Private: Visibility of the parameter. Sets what can access the contents of the variable. In this case, private defines that only methods within the class itself will be able to access and manipulate the content of the variable
  • Tray: Specifies which Class / Interface will occupy that memory space. That is, it defines the types of objects that memory space can be assigned to.
  • tray: name of the variable, that is, the space that memory defined.

There are three important points to note in the given code:

  • The Board and Player Classes must have been set to other internal or external class (Inner Class)
  • Class parameters (global variables, which define class properties) were only declared , not initialized.
  • Parameters have been declared as Private , which is a good practice (visibility control, decoupling) in object-oriented programming.
  • 2) A statement defines the types of class (or primitive) that a memory space, allocated to a variable, can be assigned. This means that you did not instantiate (actually "started") the variable, you just assigned a name to that memory space and defined its generic type.

    3) Since the parameters were set to Private , they can only be accessed and assigned through public methods (the famous Setters and Getters) or through the class constructor. The specific method you can use depends on your application, but usually the builder is used to initialize the object, and Setters and Getters to update and query the object. It is important to mention that if you instantiate the class without providing the attributes of board, player1 and player2 and try to use the Get methods you may encounter some errors.

    Public Jogo (Tabuleiro tabuleiro, Jogador jogador1, Jogador jogador2) {
        this.tabuleiro = tabuleiro;
        this.jogador1 = jogador1;
        this.jogador2 = jogador2;
    }
    

    Assuming you have empty constructors in your other classes that also accept assignment through setters and getters, you can create an object with neutral parameters and then assign values with getters and setters

    Public Jogo () {
        this.tabuleiro = new Tabuleiro();
        this.jogador1 = new Jogador();
        this.jogador2 = new Jogador();
    }
    
    public void setJogador1 (Jogador jogador) {
        this.jogador1 = jogador;
    }
    public Jogador getJogador1 () {
        return jogador1;
    }
    public void setJogador2 (Jogador jogador) {
        this.jogador2 = jogador;
    }
    public Jogador getJogador2 () {
        return jogador2;
    }
    

    Ah ... I find it interesting to quote that it is not good practice to define and instantiate Scanner within a class, it is better to use a Scanner instance defined in your Main since otherwise you risk creating several references to the Standard Input (That System.in you passed as an attribute) simultaneously in your code, and may end up unintentionally closing it.

        
    04.04.2018 / 15:13