Basically this is one of the concepts of object orientation, encapsulation, in which you can define your variable as private, and create the getter and Setter to be able to access it from another class
ex:
class Pessoa {
String nome;
public String getNome() {
return this.nome;
}
public void setNome(String s) {
this.nome = s;
}
}
Main class
class main() {
public static void main(String[] args) {
Pessoa pessoa = new Pessoa();
pessoa.setNome("João");
System.out.println(pessoa.getNome());
}
}
In this situation I created the class Person and set within it a private variable of the type String called name, in which I want to access it outside the class Person, for this I created the Setter and getter var person, making it possible to access it in other classes
In the former I gave, I transferred it from the main class, created an object of the type Person, and set a name to it "John" from the method setNome()
of the class Person, dps displayed a name that I passed the same on the screen through the method getNome()
Also: https://answall.com/q/44739/101 and https://answall.com/q/40416/101 and https://answall.com/q/212300/101 and https://answall.com/q/129353/101 and https://pt.stackoverflowcom/q/204355/101 and https:///pt.stackoverflowcom/q/45285/101 and https://en.stackoverflow.com/q/133924/101 and https://answall.com/q/202227/101 and has a number of members who I will not post here.
– Maniero
There is only one that already answer this. I didn’t even need a survey, at the time I was typing appeared all of them.
– Maniero