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.
Computer scienceProgramming languagesJavaCode organizationObject-oriented programmingClasses and objectsClasses and members
Multiple constructors
Creating a class with integer attribute and two constructors
Report a typo
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;
}
}
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.