0

Here is a problem I face when maintaining and old application, below are the code for demonstration: First, I created a class with field innerComp valued by instance of an anonymous class and a field adjustNum which is used in a method of innerComp:

public class InnerComparator {
private int adjustNum = 0;

public InnerComparator() {

}

public InnerComparator(int adjustNum) {
    this.adjustNum = adjustNum;
}

public void setAdjustNum(int adjustNum) {
  //        adjustNum = adjustNum;
      this.adjustNum = adjustNum;
}
private Comparator innerComp = new Comparator() {
    @Override
    public int compare(Object o1, Object o2) {
        System.out.println(adjustNum);
        return adjustNum;
    }
};

public Comparator getInnerComp() {
    return innerComp;
}

public void sort(List list) {
    Collections.sort(list, innerComp);
}

}

Here demonstrate how I use it:

public class CheckInnerClassUpdate {
public static void main(String[] args) {
    InnerComparator comparator = new InnerComparator();
    List<String> list = Arrays.asList(new String[]{"1", "2", "3"});
    comparator.sort(list);
    comparator.setAdjustNum(1);
    comparator.sort(list);
    InnerComparator comparator2 = new InnerComparator(1);
    // comparator.sort(list);
    comparator2.sort(list);
}

}

The result is: 0 0 1 1 1 1

(Obsolete) The result is: 0 0 0 0 0 0 I would like to ask for the mechanism that generate the result. The result seems show that the adjustNum pass to the instance of the anonymous class is referring to the init value of the adjustNum. In the sample code, even if I pass the value via the constructor, the value pass to the innerComp is still 0. There is a missing piece of knowledge that I don't know and hopefully someone could help. Thank you.

(Updated) As mentioned answer, typo cause the confusion. I have fix the code and comment out the original code.

3
  • 2
    I honestly really don't understand what you are trying to do and your problem description. But i can tell you that your public void setAdjustNum(final int adjustNum) currently is pointless and does nothing because you just assign the passed argument to itself: adjustNum = adjustNum. Maybe you meant this.adjustNum = adjustNum; but as i don't know what end result you even expect I can't tell you if this will lead to it. Commented Jun 15, 2023 at 10:25
  • Anonymous classes cannot access their enclosing classes variables unless they are final, as much it will not be considered the same as the enclosing class variable (different declared variable)if the variable has exactly the same name.It appears you are trying to pass out of the anonymous class a value in a variable that is declared in its outer enclosing class. Commented Jun 15, 2023 at 10:44
  • Sorry, all. I find there is typo for my code and I don't have any problem with it now... Commented Jun 16, 2023 at 1:38

1 Answer 1

1

I found at least an error in your code:

public void setAdjustNum(int adjustNum) {
    adjustNum = adjustNum;
}

This method does nothing because it resets adjustNum passed as parameter to the function. If you need to set the adjustNum field of the class your code must be updated to

public void setAdjustNum(int adjustNum) {
    this.adjustNum = adjustNum;
}

So the this.adjustNum refers to the property of the instance, while adjustNum (without this) refers to the parameter passed to the function. In this case you really set the adjustNum calling setAdjustNum method

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, Davide. I also find the last line is not using comparator2 as I wanted to. Sorry for the silly mistake.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.