Search code examples
javaclassmethodsprintlnmain-method

Getting ExamQuestion@143c8b3 as print out in console


I have a have a class that has a method which takes a string. Then I have a method which returns the value of the above method. When ever I get the return value in my main class and print it the printed value is "ExamQuestion@143c8b3". How do I get it to print correctly?

Thanks


Solution

  • Whenever you get the format "@" this is the default format of an object's toString() method.

    Calling System.out.println(question); calls ExamQuestion.toString(). If the method isn't overridden, the version in the super class will be called, in this case it will be Object's version.

    So that's why you get ExamQuestion@143c8b3.

    To fix this, put the following method in your ExamQuestion class:

    public String toString() {
        // return a string that means something here
    }