What is the use of get and set methods in a String array?

Asked

Viewed 2,026 times

1

I am not able to fill them and show the result on the screen with the use of these methods. To show what you have in the array I used another method the listArray and to insert data in the array I used addArray.

What is the set and get in the array for, someone explain it to me with java code, 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
  • 1

    Could you add the code snippets that you refer to, please?

1 answer

3


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

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

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

Example of the use of 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 it would not be possible to access the attribute cursos from the instance aluno created above, if there were no method getCursos().

I still can’t understand...why you used get and not set? student.getCursos()[0] = "Courses0"; ?

The doubt above was made in a comment and I found very pertinent, because really when we are learning there can be this confusion.

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

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

Change the reference means the following:

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

When using the setCursos(), changes the reference stored by the attribute cursos. Therefore, the attribute cursos 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.

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

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

Related question: Why use get and set in Java?

  • Thank you, but I still can’t understand, I’ll have to study a lot... And because using get and set I already knew just did not understand the function of it in the vector. Understand?

  • Yes. I understand your question. I am elaborating an answer. I will do my best to get you out of here with this understood.

  • @Alinegonzaga, I made an edition in the reply.

  • I understood more or less. The rest is up to me...

Browser other questions tagged

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