4

How can I update an object in class A from a method in class B without using the return?

for example:

public class A {
//my main class

private javax.swing.JTextField txtField1;
//a text field (txtField1) is initialized in this class and drawn

}


public class B {

public void doSomething(){

    //does something and updates the txtField1 in class A

}

}

and once again, I do not wish to use return since my return is already returning another value from the same method.

3 Answers 3

5

There are many ways you could achieve this. The simplest would be to pass the object into the method in class B:

public void doSomething(JTextField fieldToUpdate){
   //your logic here
   fieldToUpdate.setText("something");
}

Then you can just update fieldToUpdate directly. This is not a great design pattern since it directly exposes control of a variable owned by 1 class to another.

Another alternative is to pass the instance of Class A into the method and call public methods on it:

public void doSomething(A aInstance){
    //your logic here
    aInstance.setText("something");
}

then in class A you'd need to define

public void setText(String text){
   txtField1.setText(text);
}

This is a little better since class B doesn't have direct access to the internals of Class A.

An even more encapsulated response (though probably overkill for a case this simple) is to define an Interface and pass an instance of a class that implements the interface to the method in class B:

public void doSomething(TextDisplayer txt){
  //your logic here
   txt.setText("something");
}

then in class a:

public class A implements TextDisplayer{

 public void setText(String txt){
  txtField1.setText(txt);
}

}

then the interface:

public interface TextDisplayer{
 public void setText(String txt);
}

The advantage of this approach is that it keeps class B completely decoupled from the class A. All it cares about is that it is passed something that knows how to handle the setText method. Again, in this case it is probably overkill, but it is the approach that keeps your classes as decoupled as possible.

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

1 Comment

Thank you for your answer. What if aInstance is inside of a loop? How do I make sure it returns from setText method to doSomething method?
0

You either need to call a method in class A, or make the text field static (bad idea).

Depending on usage, class A could instantiate a B as a swing worker/etc. and give B the specific information it needs. It could also be the other way around, B instantiates an `A.

Comments

0

assume you have a public setter method in A to change the value of txtField1. (because the property you want to change has keyword "private")

say in A, you have

public void setTxtField1Value(String newValue){
this.txtField1.value=newValue; // using the right method in api. I am not familiar with gui..
}

then in B, the method would be:

public class B {

public void doSomething(A a){

    //does something and updates the txtField1 in class A
a.setTxtField1Value("foobar");

}
}

1 Comment

Thank you for your answer. What if a.setTxtField1Value("foobar"); is inside of a loop? How do I make sure it returns from setTxtField1Value method to doSomething method while the loop is true?

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.