简体   繁体   中英

Scope of global variables in a 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 question provided by the course I am taking, I am asked what is the value of variable b after executing inc(b) .

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

The answer is 5, rather than 6, which I understand is because Java is pass by value and all the arguments in the method inc is simply forgotten afterwards.

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

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

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

In your inc method, you're not changing the field called b at all. You've got a parameter called b , so every time the code refers to b within the method, it refers to the parameter instead of the field. This is called shadowing . 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 increment the field instead.

Basically, you need to think about what you want the inc method to do:

  • Is it meant to increment its parameter and return the new value? If so, it might as well be static - it doesn't interact with any state of the instance
  • Is it meant to increment the field and return the new value? If so, it should be parameterless - it's not using the parameter.
  • Is it meant to set the field to the parameter value and then increment it? If so, I would strongly advise a change in design (I know this is only an example) and definitely a change in parameter name.

Method variables are local variables. Their scope is limited to method execution. If you want class variable to change, you have to assign it to class variable by doing either one of below, like it happens in setter case.

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

or

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

and then

b = inc(b);

It sounds like you're not understanding OOP (object-oriented programming).

Set method is a member of a class, which keeps track of internal changes.

By using a setter, you're changing the contents of an instance of a class. It's not "forgotten" because an instance of the class is created first (whereas a function isn't).

See object creation for more information on instances of classes.

You just stumbled upon the difference of built-ins ( boolean, char, byte, short, int, long, float, double ) and Objects. As you said already: Java is pass-by-value . Always. There is, however, a subtle difference betweeen built-ins and objects. When you pass an object as parameter (like you do in the setter), you actually pass the value of the reference (ie the memory location) and not the actual object itself. Also, the use-cases are different in the two examples: one method ( inc ) modifies a parameter, whereas the other method ( setName ) modifies an attribute of the object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM