Consider the following code:
public static void main(String[] args) {
File file = new File("C:\someFile.txt") {
public void doStuff() {
// Do some stuff
}
};
file.doStuff(); // "Cannot resolve method"
}
When we try to call our newly defined method doStuff()
, it isn't possible. The reason for this is that file
is declared as an object of type File
and not as an instance of our new, anonymous child class.
So, my question is, is there any 'nice' way to achieve this behaviour? Other than the obvious (which is to just, declare the class properly).
See Question&Answers more detail:os