Scope of global variables in java class and their behavior in methods of the same class

I am new to programming and Java and I have some confusion regarding the behavior of global variables in methods of the same class. In an exercise in the course I am taking, I am asked what the value of b is after execution inc(b)

.

    int b;
    int inc(int b){
        b++;
        return b;
    }
    b = 5;
    inc(b);

      

The answer is 5, not 6, which I understand because Java loops by value and all method arguments are inc

just forgotten after that.

On the other hand, in the java class it is recommended to write set and get methods for all instance variables. Then my question is, why is a setter able to modify an instance variable and maintain its value outside of the setter? In other words, why is the variable "forgotten" in the above example, but "remembered" in the given method?

    public void setName ( String n ) {
        name = n;
    } 

      

+3


source to share


4 answers


In other words, why is the variable "forgotten" in the above example, but "remembered" in the set method?

In your method, inc

you don't change the field called b

at all. You have a parameter b

, so every time code refers to b

inside a method, it refers to a parameter instead of a field. This is called shading. If you change it to:

int b;
int inc(int b) {
    this.b++;
    return this.b;
}

      



... then the parameter will be ignored and it will increase the field instead.

Basically, you need to think about what you want to do inc

:

  • Is this the purpose of increasing its parameter and returning a new value? If so, it can be static as well - it doesn't interact with any state of the instance
  • Is it meant to increment a field and return a new value? If so, it must be parameterless - it doesn't use a parameter.
  • Is this field supposed to set a parameter value and then increase it? If so, I highly recommend changing the design (I know this is just an example) and definitely changing the parameter name.
+5


source


Method variables are local variables. Their scope is limited by the execution of the method. If you want to change a class variable, you must assign it to a class variable by executing either one of them, as is the case with setters.

 void inc(int b){
        b++;
        this.b=b ;        
  }

      

or



int inc(int b){
        b++;
    return b ;        
  }

      

and then

b = inc(b);

      

+1


source


It looks like you don't understand OOP (Object Oriented Programming).

The Set method is a member of the class that tracks internal changes.

Using the installer, you change the contents of the class instance. This is not "forgotten" because the class is instantiated first (whereas the function is not).

For more information on class instances, see creating objects .

0


source


You just stumbled upon the difference between inline functions ( boolean, char, byte, short, int, long, float, double

) and objects. As you said: Java is about bandwidth . Is always. However, there is a subtle difference between inlines and objects. When you pass an object as a parameter (as you do in the setter), you are actually passing the reference value (that is, the memory location), not the actual object itself. Also, the use cases are different in the two examples: one method ( inc

) modifies the parameter, while the other method ( setName

) modifies the object's attribute.

0


source







All Articles