+ 1

When can a method change the value of a variable and when it can't?

22nd Sep 2017, 6:38 PM
Aboelazm Mohamed
Aboelazm Mohamed - avatar
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.
22nd Sep 2017, 6:45 PM
Tashi N
Tashi N - avatar
+ 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
22nd Sep 2017, 6:47 PM
Rrestoring faith
Rrestoring faith - avatar
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?
22nd Sep 2017, 7:32 PM
Aboelazm Mohamed
Aboelazm Mohamed - avatar