Either write a constructor like this:
B(A newA) {
this.newA = newA;
}
But this is kind of stupid to call and always put the A class into the constructor.
Or do it the elegant way with a singleton and write your classes like that:
Class A extends javax.swing.JFrame{
private static final A INSTANCE = null;
public static void A getInstance() {
if (INSTANCE == null) {
INSTANCE = new A();
}
return A;
}
private A() {
myTextArea = new JtextArea;
}
public void setArea(String mess){
myTextArea.Append(mess);
}
}
Class B
Class B{
A newA;
B( ){
newA = A.getInstance();
}
//i have this line below which is supposed to append text in //myTextArea in class A
newA.setArea(“message”);
}
With this solution you always get the A class you entered the text into no matter where in your program you call the A.getInstance().
But only use this if you need one object of the A class in the whole program, you CANNOT create a second one.