0

And can I create custom class objects and make them behave the same way ? e.g -

If i have a class:

class MyCustomClass{
    int myDisplayValue;
    public MyCustomClass(int value){
        this.myDisplayValue = value;
    }
}

and I use it as:

class TestClass{
    public static void main(String[] args){
        MyCustomClass testObj1 = new MyCustomClass(8);
        System.out.println(testObj1); // prints the class name with some random text
        Integer testObj2 = new Integer(8);
        System.out.println(testObj2); // prints the int value '8'
    }

Is there a way to make testObj1 print '8' as well? I realize that this might not be useful in any way and there are other ways of printing values more safely, I just want to know if this is possible.

Ravi Agrawal
  • 26
  • 1
  • 3

1 Answers1

0

Override toString method and return string of your integer.

 @Override
 public String toString(){
      return String.valueOf(myDisplayValue);            
 }