+ 1
When can a method change the value of a variable and when it can't?
3 Answers
+ 10
It depends on where the method is called from:
- In the same class: any access modifier (private, protected, public).
- From another class in the same package or from a class that extends the method's class: protected.
- From another class in another package, the other class doesn't extend the method's class: public.
+ 6
I'm not sure if I understood this question, but:
A method can change the value of a variable if:
* The variable can be accessed
* If the variable is final and has not already been initialized
* The value is of the same (or sub) type as the variable
0
I mean for example:
int x=1;
void change() {x=2;}
This example wonot change the value of x but sometimes I found some methods change the value of a variable.
Is there something like that or I misunderstood the code?