Where did you get that code from? A pretty awful bit of design, with non-private fields.
What you have is somebody trying to make an immutable class. They haven't succeeded. Make the following changes:
class Test becomes "final class Test"int a becomes "private final int a"Add a public int getA() method. The usual sort.Where you write ob1.a or ob2.a, that becomes ob1.getA() or ob2.getA()Now you have an immutable class, which actually is immutable. You cannot change anything in it.
You cannot add 10 to the value, but what you can do is to add 10 to 2. Your incrByTen method does that; it adds 10 to 2, then it
creates a new object with 12 in.
In an immutable class, you cannot change any of the values. What you can do, however, is create a new object. That is what the incrByTen method does. Every time you use that, you get a new object; if you start from 2 you get an object with 12, then an object with 22, then an object with 32 . . . In this instance you are giving the name ob2 to the new object. Then you are doing the same again.
Please maintain indentation in your code, and use the
code button.
You should end up with something like this