Attribute declaration in a Java class

Asked

Viewed 302 times

1

Why the statements of these variables were 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);

    ...
}
  • 1

    Rita, this is a standard way to create private visibility class fields, only without initializing. In this sense it differs from Scanner entrada. Can you be more specific in doubt?

1 answer

0

Well, let’s look at a fragment of the code:

private Tabuleiro tabuleiro
  • Private: Parameter visibility. Sets what you can access contents of the variable. In the case of, private defines that only methods within the class itself will be able to access and manipulate the contents of the variable
  • Board: Specifies which Class/Interface will occupy that memory space. I mean, defines the types of objects that that memory space can be assigned.
  • board: name of the variable, that is, of the space that memory defined.

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

  1. The Classes Tray and Gambler must have been defined in other external or internal class (Inner Class)
  2. Class parameters (global variables, defining class properties) were only declared, uninitialized.
  3. The parameters have been declared as Private, what 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 didn’t commit (effectively "started") the variable, you just assigned a name to that memory space and defined its generic type.

3) How the parameters were defined as Private, they can only be accessed and assigned through public methods (the famous Setters and Getters) or through the class builder. The specific method you can use depends on your application, but usually you use the constructor to initialize the object, and Setters and Getters to update and query the object. It is important to mention that if you instill the class without providing the attributes of board, player1 and player2 and try to use the methods Get 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, which also accept attribution 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 mention that it is not a good practice to define and instantiate Scanner within a class, it is better to use an instance of Scanner defined in your Main since, otherwise, you risk creating several references to Standard Input (That System.in you passed as an attribute ) simultaneously in its code, and may end up closing it unintentionally.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.