Forum Stats

  • 3,768,732 Users
  • 2,252,842 Discussions
  • 7,874,701 Comments

Discussions

Updated value of variable from another class.

3243443
3243443 Member Posts: 2
edited Oct 17, 2016 9:38PM in New To Java

Tried to find this on many forums but can't find the solution or reason for the solution. I'm trying to get the updated value of a variable from other class by it's reference. The class containing the variable gets the updated value but the other class which creates a reference of the previous class still gets the old value. Here is a sample code:

class A {

     private boolean state;

     boolean getState() {

          return state;

     }

}

In the above class, the value of boolean variable state, keeps on changing by an ActionEvent.

class B {

     void go() {

          A a = new A();

          while(true) {

               if(a.getState()) {

                    //CODE

               }

          }

     }

}

Here, a.getState(), never returns true, even when class A  has contains updated state. Please suggest.

Here are some solutions that I found but can't find the reason::

1:

class B {

     void go() {

          A a = new A();

          while(true) {

               System.out.println(a.getState()); // strange, but works

               if(a.getState()) {

                    //CODE

               }

          }

     }

}

2:

class B {

     void go() {

          A a = new A();

          A aa;

          while(true) {

               aa = a; // again, works!

               if(a.getState()) {

                    //CODE

               }

          }

     }

}

Please suggest !!!

Tagged:
3243443Jonathan Ross

Best Answer

Answers

  • karianna
    karianna Member Posts: 205 Blue Ribbon
    edited May 23, 2016 4:21AM Accepted Answer

    I'll refer you to the canonical article on this: Java is Pass-by-Value, Dammit! - Scott Stanchfield

    32434433243443Jonathan Ross
  • Unknown
    edited Aug 22, 2016 10:38PM
    class A { private boolean state; boolean getState() { return state; }}In the above class, the value of boolean variable state, keeps on changing by an ActionEvent.

    There is NOTHING in what you posted that shows 'state' ever gets set to ANY value at all.

    And since it is 'private' with no 'setState' method it won't get changed.

  • dvohra21
    dvohra21 Member Posts: 14,250 Gold Crown
    edited Sep 23, 2016 9:00PM

    Use the following class definitions.

    class A {

        public boolean state;

       public  boolean getState() {

              return state;

         }

    }

    class B {

       public  void go() {

              A a = new A();

                   if(a.getState()) {

                        //CODE

              }

         }

    }

  • Jonathan Ross
    Jonathan Ross Member Posts: 12
    edited Oct 12, 2016 12:09PM

    Without a more complete listing of your code (or a more clear explanation of what you are trying to achieve), it is hard to help you. But in your code, as written, there is no way a.state (or aa.state) can get modified - they are local variables, they don't escape the scope of the method go(), and so they cannot possibly be mutated by another thread.

    Try to write a program that more accurately captures what you are trying to do, and maybe we can help you further?

  • Jiri.Machotka-Oracle
    Jiri.Machotka-Oracle Member Posts: 5,078
    edited Oct 12, 2016 3:31PM

    I'd rather keep the attribute private, but change the constructor method to set a default value to the attribute (whichever makes more sense) + add a new constructor that would allow to get the value for the state attribute.

  • Unknown
    edited Oct 12, 2016 4:22PM
    add a new constructor that would allow to get the value for the state attribute.

    I think you mean add a new 'method'?

    Besides - this thread is 5  months old and appears to have been abandoned by OP.

This discussion has been closed.