Sharing getter method across classes

I have 2 classes (A & B) as below. How to share Class A getter method value to Class B ? Pls suggest.

Class A has id getter & setter:

public class A {

@Id
public Integer id;

public A() {
}

public A(Integer id) {
    this.id = id;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}
}

Class B has value getter & setter. Inside value getter method, I need to access Class A getId() value. Currently, I am getting null.

public class B {

private Double value;

public B() {
}

public B(Double value) {
    this.value = value;
}

public Double getValue()  {

A a = new A();
System.out.println("class A id value:" + a.getId());

    return value;
}

public void setValue(Double value) {
    this.value = value;
}   
}

1 answer

  • answered 2017-11-14 23:52 Ian

    You are getting null because you are initialising a = new A(); You are not passing the Id value eg: new A(123);