A class groups state and the operations that use it. A constructor initializes a new object; it has the class name and no return type. When a parameter has the same name as a field, this.field makes the destination explicit. A private field keeps outside code from changing state without passing through the class's methods.
Use those methods to preserve rules, such as rejecting a negative addition before it changes a count. Objects remain independent only when you construct them separately. Copying a reference gives two variables access to one object. A toString override provides a predictable domain description instead of the default identity-based text. These examples put small classes inside Solution to fit one source file. They are ordinary nested Java classes, not a replacement object model.