Home>

I am creating a calculation program. Specifically, if there are a and b, I want the calculated value of a to be a + b and the value of b to be 0.

The value of a is output correctly, but the value of b is not updated

Corresponding source code
public class Number {
    public int number;// your number
    // Let the argument number (num) be your own number (number).
    public Number (int num) {
        number = num;
    }
    public void absorb (Number b) {
    // TODO: Write the process here
        number = number + b.getNumber ();
        b = new Number (0);
        // return your own numbers
    public int getNumber () {
        return number;

Output code
public class absorb {
    public static void main (String [] args) {
        Number s1 = new Number (1);
        Number s2 = new Number (3);
        s1.absorb (s2);// s1 absorbs s2
        System.out.println ("s1 =" + s1.getNumber ());
        System.out.println ("s2 =" + s2.getNumber ());
What I tried

Please describe here what you have tried for the problem.

Supplementary information (FW/tool version, etc.)

Please provide more detailed information here.

  • Answer # 1

    The following part of the output code

     s1.absorb (s2);// s1 absorbs s3


    I'm calling absorb of s1

      public void absorb (Number b) {
        // TODO: Write the process here
            number = number + b.getNumber ();
            b = new Number (0);


    You haven't returned the variable b here to the caller, right? (It's void)

    So

     System.out.println ("s2 =" + s2.getNumber ());


    However, the number of s2 has not changed.

    It is necessary to return the value of absorb and set that value to the number of s2.

  • Answer # 2

    --b = new Number (0);
    + b.number = 0;