9 Replies Latest reply on Apr 15, 2007 2:54 PM by 800282

    Using an object in method after it has changed since constructor

    807599
      Hi, I've got a class (classX) that accepts a particular object (object1) as a parameter in the constructor. Once the new object of classX has been created, there is a change to the value of object1.

      When calling methods within objectX, the old value of object1 is used so results are not what would be expected.

      Is there some way to make a method recheck the values before execution?
        • 1. Re: Using an object in method after it has changed since constructor
          800282
          Could you post a small, executable example of the behavior?
          • 2. Re: Using an object in method after it has changed since constructor
            807599
            What i mean is as follows:

            i create two new objects as follows:

            Type name1 = new Type(true);
            Type name2 = new Type(false);

            LogicAnd and1 = new LogicAnd(name1, name2);
            >>
            System.out.println(and1.getAnswer());

            As is the code works correctly and gives the correct answer of false,
            if however the following line at the >>

            name2 = new Type(true);

            then although one would expect an answer of true, false is returned all the same. I understand that this is occuring because the object is changing after it is passed to LogicAnd in its constructor, but i was wondering if there is some way around this?
            • 3. Re: Using an object in method after it has changed since constructor
              807599
              name2 is not an object.

              name2 is a 'reference' to an object.

              So when you wrote "name2 = new Type(true);" near the end,
              you did not CHANGE the original object. Rather, you created a new object.
              So of course you see the behavior you get.
              I understand that this is occuring because the object is
              changing after it is passed to LogicAnd in its constructor
              No, the "object" did not change. Your reference did.
              Let me quote your code, and draw out what references are pointing to who, at each line:

              >
              Type name1 = new Type(true);
              name1 --> TypeObject#1

              >
              Type name2 = new Type(false);
              name1 --> TypeObject#1
              name2 --> TypeObject#2

              >
              LogicAnd and1 = new LogicAnd(name1, name2);
              name1 --> TypeObject#1
              name2 --> TypeObject#2
              and1 --> LogicAndObject containing TypeObject#1 and #2

              >
              name2 = new Type(true);
              name1 --> TypeObject#1
              name2 --> TypeObject#3
              and1 --> LogicAndObject containing TypeObject#1 and #2
              • 4. Re: Using an object in method after it has changed since constructor
                807599
                Hmmm, how should i go about it then?
                • 5. Re: Using an object in method after it has changed since constructor
                  807599
                  Go about what?

                  You need to read up on the concept of "references" and "objects" in Java.
                  Your entire goal makes no sense.
                  • 6. Re: Using an object in method after it has changed since constructor
                    800282
                    ...
                    name2 = new Type(true);
                    ...
                    As Kathy already said: you're not changing the contents of a Type in LogicAnd by this line.
                    You're merely assigning a new instance to the variable name "name2".

                    Compile and run this example:
                    class Main {
                        public static void main(String[] args) {  
                            Type name1 = new Type(true);
                            Type name2 = new Type(false);
                            LogicAnd la = new LogicAnd(name1, name2);
                            System.out.println(la.getAnswer());
                            name2.value = true;
                            System.out.println(la.getAnswer());
                        }
                    }
                    
                    class LogicAnd { 
                        Type a, b;
                        public LogicAnd(Type a, Type b) { this.a = a; this.b = b; }
                        boolean getAnswer() { return a.AND(b); }
                    }
                    
                    class Type {
                        boolean value;
                        public Type(boolean value) { this.value = value; }
                        public boolean AND(Type that) { return this.value && that.value; }
                    }
                    • 7. Re: Using an object in method after it has changed since constructor
                      807599
                      Hi, i've solved the problem i had, it was unrelated to the code i gave, which i was using to test the functionality of another method.

                      Thanks for the example all the same, im still starting off Java and am very shaky with the concept of objects having just moved to oop from a base in iterative programming. I will give it a spin and hopefully it will help me clarify some things.
                      • 8. Re: Using an object in method after it has changed since constructor
                        800282
                        If you do
                        Type name1 = new Type(true);
                        Type name2 = new Type(false);
                        and1 = new LogicAnd(name1, name2);
                        Your object references will look like this:
                                   ---------
                                   |  -----|
                        name1 -----+->|true ||
                                   |  -----|
                                   |         |
                                   |  -----|
                        name2 -----+->|false||
                                   |  -----|
                        and1 ----->|         |
                                   ---------
                        Now if you'd assign a new value to "name2", by doing
                        name2 = new Type(true);
                        Your object references will now look like this:
                                   ---------
                                   |  -----|
                        name1 -----+->|true ||
                                   |  -----|
                                   |         |
                                   |  -----|
                                   |  |false||
                                   |  -----|
                        and1 ----->|         |
                                   ---------

                                      -----
                        name2 ------->|true |
                                      -----
                        Note that the old false Type will still be in "and1", and that "name2" will just get a new instantce of a Type object assigned to it.
                        • 9. Re: Using an object in method after it has changed since constructor
                          800282
                          Hi, i've solved the problem i had, it was unrelated
                          to the code i gave, which i was using to test the
                          functionality of another method.

                          Thanks for the example all the same, im still
                          starting off Java and am very shaky with the concept
                          of objects having just moved to oop from a base in
                          iterative programming. I will give it a spin and
                          hopefully it will help me clarify some things.
                          Good to hear you solved it. I hope you also understand what Kathy and I meant in our answers.
                          Good luck.