0
public Exemplo {
private int name;
public Exemplo(int name)
{
this.name = name;
}
public void setName(int name)
{
this.name = name;
}
public int getName()
{
return name;
}
}
I want to understand how the variable private int name;
and the local variable of the parameters works.
From what I understand, when the variable within the parameter has the same name as the instance variable, the field of this method will reference the local variable instead of the instance variable. " The local variable simulates the instance variable in the method"
My question is: what value will be implemented in the method setName
, using the argument this.name = name;
it will be allocated in the instance variable or will be assigned only to the method getName
? where these values go, to the instance or to the constructor?
And besides, if, for example, I had a counter as an instance variable:
private int contador = 0;
And had a method to give contador++
, this method would have to be referenced in the manufacturer?
Like, I create an object and a objetoTest
and, in the latter, have a repeat loop that returns the amount of people with white hair. How would I do? I could not understand.
welcome to Sopt, I formatted your question and edited the first paragraph pq was a little confused, check if I understood correctly, if not you can refuse the edition. = D
– Icaro Martins
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).
– Maniero