-3

Being new to Java but an old hand on older procedural languages and structured programming, I have a question on how to accomplish something in Java

I have three classes, let's say they're called CLASSA, CLASSB, and TESTCLASSA. CLASSA has a class definition with instance variables, and a constructor for some data. TESTCLASSA creates an instance of CLASSA and passes data to CLASSA by creating an instance of the object for CLASSA. SImiliarly CLASSB has another class definition with instance variables, and a constructor for some data. TESTCLASSA creates an instance of CLASSB and passes data to CLASSB by creating an instance of the object for CLASSB. I am trying to access CLASSB's data from CLASSA. Can someone suggest how I might go about doing this. Many thanks in advance for any assistance/suggestions you can provide.

Wayne Hann

Wayne Hann
  • 11
  • 1
  • 3
  • use `getters` e.g. `public String getMyData(){ return this.myData; }` – Lino Feb 26 '18 at 09:46
  • Possible duplicate of [How do getters and setters work?](https://stackoverflow.com/questions/2036970/how-do-getters-and-setters-work) – Lino Feb 26 '18 at 10:14

2 Answers2

0

Either declare the variable as public:

public class classA {
  public Integer data;
}

or create a public getter (preferred), such as:

 public class classA{
    private Integer data;

    public Integer getData() {
      return data;
    }
  }
  • 1
    java naming conventions suggest to prefix *getters* with *get* so `public Integer data(){...}` should be `public Integer getData(){...}` – Lino Feb 26 '18 at 09:53
-1

If you want to access properties of class B from class A then it's either possible that you create a object of B in the method from where you want to getData like

private String nameofA;

public String getNameofA() {
    return nameofA;
}

public void setNameofA(String nameofA) {
    this.nameofA = nameofA;
}
public String getClassBData(){
    B b = new B();
    return b.getNameofB();
}

else you create a class level instance or dependency of B type.

public class TestClassA{

    public static void main(String[] args) {
        B b = new B();
        b.setNameofB("class B Name");
        A a = new A("class A Name",b);
    }
}
class A{
    private String nameofA;
    private B b = new B(); //either this 
    public String getNameofA() {
        return nameofA;
    }

    public A(String nameofA, B b) {//or constructor
        super();
        this.nameofA = nameofA;
        this.b = b;
    }


    public void setNameofA(String nameofA) {
        this.nameofA = nameofA;
    }
    public String getClassBData(){
        B b = new B();  // or creating local instance 
                        //but here a new instance will be created
        return b.getNameofB();
    }
}

Then only you will be able to access the data of instance of B. Anyway you if your method in B is not private or protected(assuming A doesn't extend B), you can access the method by creating or passing a instance of B to the method of "A" from where you want to access.

Sagar Kharab
  • 369
  • 2
  • 18