Cannot change the value of an instance variable
I wrote this piece of code to clear my doubts, I am a little confused about this.
class Client{
int a=80;
public void setA(int a) {
this.a = a;
}
public int getA()
{
return a;
}
public static void add(int b)
{
b=15;
System.out.println(b);
}
public static void main(String[] args) {
int b=10;
System.out.println(b); //Output 10; that Ok with me
add(b); //Output 15; that Ok
System.out.println(b); // Expected output 15 but still 10
new Client().a=56;
System.out.println(new Client().a); //Expected output 56 but prints 80
new Client().setA(98);
System.out.println(new Client().a); //Expected output 98 but prints 80
System.out.println(new Client().getA()); //Expected output 98 but prints 80
}
}
Output:
10 15 10 80 80 80
Q A. In the method, the add
value is b
set as 15
. It was originally 10
. So, the final value b
should be now 15
. It still prints as 10
.
Q B. I have an instance variable a
initialized with a value 80
. I am creating a Client object and trying to change the value, as you can see in the code. But the value a
prints the same every time. The value a
should change.
source to share
You create new objects every time. Store the reference to the object in a variable.
Client client = new Client();
client.setA(56);
System.out.println(client.getA());
client.setA(98);
System.out.println(client.getA());
When you call new Client()
, you create new objects for the Client class and they are not related to each other, do not share data.
To store the value of a variable b
and make it constant between calls to static methods, declare it as a static field on the class.
class Client{
int a=80;
static int b;
and don't declare a named variable b
in your methods to prevent the variable from being shaded by the local variable.
Local variables (in your case, declared inside your method) are not persisted when your method finishes executing.
So the final b value should now be 15. It still prints as 10.
This is a different value for b because the parameter is passed by value.
public static void add(int b)
{
b=15;
System.out.println(b);
}
You can rewrite your code like this
public static void add(int anotherB)
{
b=15;
System.out.println(b);
}
and it won't compile, reinforcing that the variable in this method is different from the one declared in Main.
Rewrite it again as
public static void add(int anotherB)
{
anotherB=15;
System.out.println(anotherB);
}
and it will compile again and it should be clear that this is a different variable than in Main ().
But the print value is output the same every time. The value of a should change.
Same problem. You have two different variables with different scopes with the same name a
. You have the additional problem that you keep creating new Client () instances.
source to share
A: You are changing the value of the parameter passed to the add () method, not the variable declared in main ():
public static void add(int b)
{
b=15; // <---- This is changing the local parm
System.out.println(b);
}
public static void main(String[] args) {
int b=10; // <---- This is in the scope of main and will not be altered
System.out.println(b); //Output 10; that Ok with me
add(b); //Output 15; that Ok
System.out.println(b); // Expected output 15 but still 10
B: You created an anonymous object and assigned 56 to a, the unnamed object immediately went out of scope.
new Client().a=56; // <-- changed to 56 and then gone
Then you create another one to print 80 and it's gone right away ...
System.out.println(new Client().a); //Expected output 56 but prints 80
You create another anonymous object and assign 98
new Client().setA(98);
Leaves ...
You create two more unnamed objects and they are initialized to 80
System.out.println(new Client().a); //Expected output 98 but prints 80
System.out.println(new Client().getA()); //Expected output 98 but prints 80
}
Try this instead:
Client c = new Client();
c.a=56; // It is now 56
System.out.println(c.a); //prints 56
c.setA(98);
System.out.println(c.a); //prints 98
System.out.println(c.a); //prints 98
System.out.println(c.getA()); //prints 98
}
source to share