Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getters method is return value in same class but return 0 in another class [JAVA]

First, I’m new in java and programming. I have a project that has OOP in it. Before I start writing this question, I already search for the answer in internet. There are some question like this, but I think that’s not a same question that I really mean.

So I make an encapsulation class that can setter & getter value in ClassA. Here’s the code:

class ClassA {
    private int a;

    public void setA(int a) {
        this.a = a;
    }

    public int getA() {
        return a;
    }

    public void print() {
        System.out.println("value a in ClassA: " + a);
    }
}

I’m trying to call that getter from ClassB and initialize it in "private int a". Here’s the code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class ClassB {
    private ClassA classA = new ClassA();

    private int a = classA.getA();

    public void print() {
        System.out.println("value a in ClassB: " + a);
    }
}

And Here’s the driver code:

public class Main {
    public static void main(String[] args) {
        ClassA classA = new ClassA();
        ClassB classB = new ClassB();
    
        classA.setA(10);

        classA.print();
        classB.print();
    }
}

The output from driver code is:

value a in classA: 10
value a in classB: 0

The output that I want is:

value a in classA: 10
value a in classB: 10

In my expectation is when I setter setA with value 10 in ClassA, I can return that value from getA() into ClassB and initialize it to "int a" in that class. But when I try to print it, the value "int a" in ClassB is always set to 0 but "int a" in ClassA is initialized and print the correct value.

When I just call getA() in driver code like this:

System.out.println(classA.getA());

It’s still can print a correct value of 10, so that mean that method still can return a value.

The problem it just can’t get that return value into another class. Please help me to fix that return value from getter can be initialized in another class. Thank’s!

>Solution :

You instantiate class A two times, the instance from class B and Driver class is different, you didn’t assign value of a for instace in class B. you have to use one instance class of A

class ClassB {
private ClassA classA = new ClassA();

private int a = classA.getA();
public void setvalueOfA(int a){
   this.a=a;
   this.classA.setA(a);
  }

public void print() {
    System.out.println("value a in ClassB: " + a);
}

}

public class Main {
public static void main(String[] args) {
    ClassB classB = new ClassB();
    classB.setValueOfA(10);
    

    classB.classA.print();
    classB.print();
}

}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading