techPackets
techPackets

Reputation: 4504

Cannot change the value of instance variable

I have written this piece of code to clear my doubt, I am a little confused on 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's Ok with me
        add(b); //Output 15; that's 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 add the value of b is being set as 15. Initially it was 10. So the final value of b should be now 15. It is still printing as 10.

Q B. I have an instance variable a initialized with the value 80. I create a Client object & try to change the value as you can see in the code. But the value of a prints the same every time. The value of a should change.

Upvotes: 3

Views: 3392

Answers (3)

Joe Zitzelberger
Joe Zitzelberger

Reputation: 4263

A: You are changing the value of the parameter passed in 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's Ok with me
    add(b); //Output 15; that's 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 is gone instantly too...

    System.out.println(new Client().a); //Expected output 56 but prints 80

You create another anonymous object and assign 98 to a

    new Client().setA(98);

It goes away...

You create two more nameless 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
}

Upvotes: 1

Eric J.
Eric J.

Reputation: 150108

So the final value of b should be now 15. It is still printing as 10.

That's a different b, as the parameter is passed by value.

public static void add(int b)
{
    b=15;
    System.out.println(b);
}

You could rewrite the code like this

public static void add(int anotherB)
{
    b=15;
    System.out.println(b);
}

and it would not compile, reinforcing that the variable in this method is different than the one declared in Main.

Rewrite it again as

public static void add(int anotherB)
{
    anotherB=15;
    System.out.println(anotherB);
}

and it will again compile, and should be clear that this is a different variable than in Main().

But the value of a prints the same every time. The value of a should change.

Same issue. You have two different variables, with different scope, with the same name a. You have an additional issue that you keep creating new instances of Client().

Upvotes: 2

user784540
user784540

Reputation:

You are creating new objects every time. Store the object reference to 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 are creating new objects for class Client, and they are not connected to each other neither share the data.

To store the value of b variable and make it persistent between static method calls, declare it as a static class field.

class Client{
    int a=80;
    static int b;

and do not declare a variable with name b in your methods to prevent class variable shadowing by local variable.

Local variables (in your case, declared inside of your method) do not persist when your method is completed its execution.

Upvotes: 3

Related Questions