Is it possible to declare a method while creating an object? I ran across the following lines of code in java:
public static void main(String[] args) {
Comparator<String> comparator = new Comparator<String>() {
public int compare (String s1, String s2) {
return s1.compareToIgnoreCase(s2);
}
};
}
And it looks like that while creating the object comparator
, the code is adding a method to implement the Comparator<T>
interface. Is adding additional methods always possible when creating instances, or is it something in particular that's related to a java interface?
Thanks for any help!