So I am doing a sort here and implementing the Comparator interface using an inner class, exceptionMessage and didJsonParsingFailed are variables declared outside the inner class, now java doesn't allow accessing of local variables using inner class, so this gives me an error, but when I make these two variables as atomic, "AtomicReference" and "AtomicBoolean", in that case program runs fine, I was not able to understand the reason behind it. How does making it Atomic helps? Does java allows access of AtomicVariables in inner class.
P.S- I have to modify these variables in my catch block so I cannot make it final
Collections.sort(list, new Comparator() {
private static final String KEY_NAME = "createdDateTime";
@Override public int compare(Object o1, Object o2) {
String str1;
String str2;
Date d1 = new Date();
Date d2 = new Date();
try {
str1 = (String) ((JSONObject) o1).get(KEY_NAME);
str2 = (String) ((JSONObject) o2).get(KEY_NAME);
d1 = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss").parse(str1);
d2 = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss").parse(str2);
} catch (JSONException | ParseException e) {
exceptionMessage = "xyz";
didJsonParsingFailed = true;
}
return d1.compareTo(d2);
}
});