0

JAVA

public class A {
  string amountA;
  B b;
}

public class B {
  string amountB;

   public void setValue(String value) {
      amountB = value;       

Also, I need to set amountA = value;. Is it possible? See main method

      }
}

... main(String... args) {
   A a = new A();
   B b = a.getB(); // b is a member of A
   b.setValue("25") // this should also change 'amountA' in object 'a' to '25'
}
5
  • 1
    Note that with how your curly brackets are, B is not an inner class of A. Commented Sep 4, 2014 at 17:05
  • 1
    please post all the source code. Is class B an inner class of class A? Commented Sep 4, 2014 at 17:05
  • this code won't compile Commented Sep 4, 2014 at 17:05
  • Apparently, "outer object" here does not refer to inner and outer classes, but to a class that contains an instance of another class (composition, not nested classes). Is that correct, OP? Commented Sep 4, 2014 at 17:28
  • Thanks for the reply. Assume main() is inside another class C. A and B are independent classes ( not actual inner class ). The relation between A and B is using composition ( B instance is inside A ). Commented Sep 4, 2014 at 18:34

3 Answers 3

1

If you need to set both valueA and ValueB to the same value, it makes more sense to have a setter in class A that would set both of them :

   public void setValue(String value) {
      amountA = value; 
      b.setValue(value);
   }

You can't access an instance of A from within an instance of B, since B is not an inner class of A. You can create an instance of B that is not related at all to any instance of A.

Sign up to request clarification or add additional context in comments.

Comments

0
public class A {

    public class B {
        string amountB;

        public void setValue(String value) {
            amountB = value;       
            amountA = value; // Using A.this.amountA
        }
    }

    string amountA;

    public B createB() {
        return new B(); // Provides A.this to the B instance.
    }
}

... main(String... args) {
    A a = new A();
    B b = a.createB(); // b is created inside A
    b.setValue("25") // this should also change 'amountA' in object 'a' to '25'
}

You can use an inner class.

The class itself should better create the instance, so that A.this is set into the b.

Alternatively

B b = a.new B();

But I never ever used that.

Inner classes are practical for a container element having access to its container class.

Another solution is to make a method in A with the B object as parameter or so.

Comments

0

It sounds like you want to have A derive from B rather than contain an instance of B. That way the valueA in an instance of A and the valueA in the instance of B associated with that instance of A are really the same variable. You can also have data in A that isn't shared with B by declaring it in A. For example,

public class A extends B {
   public String amountAonly; // a string in A that's not in B
   public B getB() { return (B)this; } // typecast to treat A like B
}

public class B {
   public String amountA; // a string that's in both A and B
   public void setValue(String value) {
      amountA = value;
   }
}
...
   main(String[] args) {
       A a = new A();
       B b = a.getB(); // b is the B associated with a
       b.setValue("25"); // will also change 'amountA' in object 'a' to '25'
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.