I want to understand how the instance variable and the local variable of the parameters works

Asked

Viewed 104 times

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

  • 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).

2 answers

4

The terms are misused in the question.

There is the instance variable, the class variable, and the local variable, in addition to the parameter which is actually a local variable which is initialized by its call.

I’ve never heard the term method header at most method signature, but I understand what you mean.

There is no variable inside the parameter, there is variable that is parameter.

When there is collision of names the local variable always has priority, the this used to be used to disambiguate.

"The local variable simulates the instance variable in the method"

No. When there is no collision the instance variable can be accessed directly in the same way as the local variable, but it is only a syntax facility, the semantics is not local variable.

Values are not implemented. We are not talking about allocation there, nor is anything attributed to methods.

this.name = name; is taking the value of the local variable name, which happens to be a parameter, and is assigning to the instance variable name, only that and nothing else.

The second part of the question seems to have nothing to do with the first part, but come on.

You use in the constructor what you need to build, what you don’t need you don’t use. See more in What good is a builder?. But instance methods should not be referenced in the constructor, because until finished constructing they are not invalid.

I would start by trying to understand things as they really are, give correct names to things. The fact that this is still confusing will harm learning. The final part of the question is extremely confusing and probably not even part of the scope of this site to answer that, other than it doesn’t even seem to be part of the rest of the question. Without the most basic understanding I wouldn’t even try to insist on it.

The question starts from other potentially wrong premises, but I’m not even going to get into this merit which is something more advanced.

0

My question is, the value that will be implemented in the setName method, using the argument "this.name = name;" will it be allocated in the instance variable or will it be assigned only to the getName method? where these values go, to the instance or to the constructor?

this.name refers to the instance variable, note that in the setName you are assigning a value to the instance variable.

The setAlgumaCoise methods serve so that you can assign a value to your instance variable, to fill in the object values before, for example, saving it in the database.

The getAlgumaCoise methods are used to retrieve an instance value to do something with it, for example, display on the screen.

And besides, if, for example, I had a counter as an instance variable:

private int counter = 0;

And had a method to give a counter++, this method would have to be required to be referenced in the manufacturer?

This method does not need to be obligatorily referenced in the constructor, you can instantiate your object and then call the method, like this:

MeuObjeto meuObjeto = new MeuObjeto();
meuObjeto.contador();

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"

Actually there is no simulation here, this is only used to distinguish between the instance variable and the parameter variable of the method, when the name is repeated, otherwise the use of this is not required.

Browser other questions tagged

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