-1

I dont know whats the correct input to print so that my output can be the int values of x,y and a instead of memory space.

public class IntObject{
    public int num;

    public IntObject() {
        num=0;
    }
    public IntObject(int n){
        num=n;
    }
    public void increment() {
        num++;
    }

}
public class IntObjectTest{

    public static IntObject someMethod(IntObject obj) {
        IntObject ans =obj;
        ans.increment();
        return ans;
    }
    public static void main(String[]args) {
        IntObject x = new IntObject(2);
        IntObject y = new IntObject(7);
        IntObject a =y;
        x=someMethod(y);
        a=someMethod(x);


    System.out.print(x);
    System.out.print(y);
    System.out.print(a);

    }

}

What can I do to change so that I can get the int values?

2 Answers2

1

You should override toString method inside IntObject class if you want to print the objects contents as specified in your requirement.

You can either implement it as shown below or can generate it using the IDE(Eclipse or IntelliJ) automatically:

@Override
public String toString() {
    return "num = "+ num;
}

In Eclipse do right clcik -> source -> generate toString()

Yug Singh
  • 3,112
  • 5
  • 27
  • 52
0

You can override the Object.toString() method in your IntObject class.

@Override
public String toString() {
    return String.valueOf(num);
}
gbryce
  • 56
  • 4