What are the get and set methods in a String array?

1

I'm not able to populate them and display the result on the screen using these methods. To show what I have in the array I used another method the listarArray and to insert data in the array I used addArray.

What is the purpose of the set and get in the array, does anyone explain to me with codes in Java, please?

public class Aluno {

private String nome;
private String matricula;
private int idade;
private String[] cursos = new String[5];

public String[] getCursos() {
    return cursos;
}

public void setCursos(String[] cursos) {
    this.cursos = cursos;
}
...// outros getters e setters
    
asked by anonymous 03.02.2016 / 15:36

1 answer

2

The cursos attribute is of type String[] and is declared with the private modifier. This means that it is accessible only from within the Aluno class.

However, you may need to access it from a created instance of its class. Because of this, it is very common to use methods to make this possible. Gets and Sets is a widely used nomencaltura (mainly in Java) to refer to methods that allow you to get and change the contents of a private attribute.

Therefore, the getCursos() method does nothing more than return the cursos attribute. The setCursos() , allows you to change the cursos attribute.

Example of using get and set :

public static void main(String[] args) {
    Aluno aluno = new Aluno();

    //Utilizando o getCursos() para poder alterar o conteúdo do vetor
    aluno.getCursos()[0] = "Curso0";
    aluno.getCursos()[1] = "Curso1";
    aluno.getCursos()[2] = "Curso2";
    aluno.getCursos()[3] = "Curso3";
    aluno.getCursos()[4] = "Curso4";

    String[] novosCursos = new String[10];

    for(int i = 0; i < novosCursos.length; i++)
      novosCursos[i] = "Curso" + i;

    //Utilizando o set para alterar o atributo alunos, passando
    //a referencia de um novo vetor criado acima
    aluno.setCursos(novosCursos);
}

Note that you would not be able to access the cursos attribute from the aluno instance created above, if there was no getCursos() method.

  

I still can not understand ... why did you use get and not set?   student.getCourses () [0]="Course0"; ?

The above question was made in a comment and I found it very pertinent, because really when we are learning there may be such confusion.

The setCursos method does not change the contents of a given vector position, as you might suggest. In other words, set , although it means something to change, in this case, not is the content of a specific position.

What set does is change the reference of the cursos attribute of the Student class (knowing a little pointer can help you understand this more easily).

Changing the reference means the following:

When a new Aluno is created with new Aluno() a 5-position vector of type String is allocated and the reference of this vector in memory is stored by the cursos attribute.

When you use setCursos() , you change the reference stored by the cursos attribute. Therefore, the cursos attribute that was referencing a 5-position vector in memory is now referencing another vector, in the case of the above example, a 10-position vector.

The getCursos() method is used to access the cursos vector (note that it only returns the cursos vector). Having access to the vector means being able to manipulate it (change the content of positions), access its methods (such as length).

Because of this, to change or access a position of the vector is done getCursos()[i] . Note that if the cursos attribute was public , then aluno.cursos[i] could be done.

Related question: Why use get and set in Java?

    
03.02.2016 / 15:50