In class A, your code creates a new instance of GetterAndSetter and sets a value to the property. In class B, however, your code creates again another new instance of GetterAndSetter , then gets the value.
The instances your code works with in classes A and B are not the same - hence you don't obtain the values set in A when trying to get it in B. The instance of GetterAndSetter created in B is not used anymore after the code in B exits.
To fix this, you need to pass a reference to the GetterAndSetter instance from class A to B. You can do this e.g. by passing it as a parameter to a method of B, or by creating a new instance of A in B and calling a method that provides an instance of GetterAndSetter.
An example of the first option (pass as parameter):
Class A{ ... GetterAndSetter createAndSet(); int a = 10; GetterAndSetter gs = new GetterAndSetter(); gs.setValue(a); return gs; } ... } class B { ... void getValueFromGetterAndSetter(GetterAndSetter gs) { int c; c = gs.getValue(); ... } ... }
To connect the instances, we of course also need to have another piece of code (assuming instances of A and B exist already):
... b.getValueFromGetterAndSetter(a.createAndSet()); ...
null???