I have downloaded the solution because I wasn't able to come up with the logic for a successful program. However, I have trouble understanding some of the expressions in the provided solution. So let's take if from the beginning. The input and output are displayed in the Solution.java class, at the bottom. Now, let's follow the logic of the program and I will let you know what I don't understand. 1. Solution solution = new Solution(5); So a new solution with param = 5 is created. a. when a new solution is created, a new anonymous class is attributed to the solutionAction reference variable (line 1 in code below); 2. solution.someAction(); the someAction method of the solution class is called in the main. This, in turn calls the anonymous class's (solutionAction) someAction() method. 3. The someAction() method of someAction object is called and the param is checked; TRUE, it is bigger than 0. 4. OK, now it goes in the if statement (line 7), checks if first is empty, which is TRUE. 5. Next, it assigns a new anonymous class to the 'first' variable. a. here, the someAction() method of the abstract class FirstClass which needs to be implemented is overidden; here is my first problem: I assume the super.someAction() (line 12) method is called because of inheritance (but doesn't actually do anything) I don't understand what Solution.this.someAction() (line 13) does and what its role is. b. the getDependent class of the FirstClass is overriden but not because it's mandatory but because apparently is needed. here is my second problem: What does this class return (line 20)? More precisely, what do Solution.this and this return? I assume 'this' returns 'first' object. 6. Then, after the if statements are exited, we get to line first.getDependentAction().someAction(); From this line, I understand the following, the first object calls the getDependentAction which returns an object, either 'first' or Solution.this (what's this?). So it creates another object. 7. This finishes the first 2 lines of the main method, next the SOUT method is called etc. here is my third problem: For param = 5, when has the SecondClass method has been called so that the program displays the correct "The number of created Action objects is " ?
private Action solutionAction = new Action() {
       //write your code here
           private FirstClass first;
           private SecondClass second;

           public void someAction () {
               if (param > 0) {
                   if (first == null) {
                       first = new FirstClass() {
                           @Override
                           public void someAction() {
                               super.someAction();
                               Solution.this.someAction();
                           }

                           @Override
                           public Action getDependentAction() {
                               System.out.println(param);
                               param--;
                               return param > 0 ? Solution.this : this;
                           }
                       };
                   }
                   first.getDependentAction().someAction();
               } else {
                   if (second == null) {
                       second = new SecondClass() {
                           @Override
                           public void someAction() {
                               sb.append(SPECIFIC_ACTION_FOR_ANONYMOUS_SECOND_CLASS_PARAM).append(param);
                               super.someAction();

                           }
                       };
                   }
                   second.someAction();
               }
           }