Creating a class with integer attribute and two constructors

Report a typo

Consider a class that models a certain entity in your Java application. This class should have a single integer attribute. The class should have two constructors: a default constructor that sets the attribute's value to 1, and another constructor that accepts an integer parameter and uses it to set the attribute's value.

Fill in the gaps with the relevant elements
 class  {
     x;

    MyClass() {
        .x = 1;
    }

    MyClass(int i) {
        this.x = i;
    }
    
    public String toString() {
        return "Value: " + this.x;
    }
}
intMyClassthispublic

Create a free account to access the full topic