We have discussed inner classes and local classes - inner class is a class in a class, local class is a class in method, loop or any block between two parentheses. Anonymous class is a class in an expression or a statement. Anonymous classes are used when it has to be used only once and to override the methods. It does not have any class name so it is declared and instantiated in only one statement.
Anonymous classes don't have constructors and have same restrictions as in local classes -
- The anonymous class can not be accessed from anywhere out from the current block, as it doesn't have any class name.
- The anonymous classes can access the variables of the enclosing block, but can not change the value of the variables. The anonymous class treats all the variables in the enclosing block as final.
- The anonymous classes can not have static variables.
- The variables to be accessed by anonymous class are effectively final. It doesn't matter if the variable declared final or not, if it is to be accessed by the anonymous class, it has to be final. Even, the value of the variable can not be changed after the anonymous class statement anywhere.
Anonymous class is a statement, so its declaration is followed by a semi-colon (;) and also doesn't have any constructor as it doesn't have its own name. Anonymous class can override the methods and can also have its own methods, but its non-overridden methods can not be accessed by anywhere outside from the anonymous class.
public class Simple {
void printText() {
System.out.println("This is a simple text!!");
}
}
Above class is the normal class and have one method to print the simple text.
public class TestAnonymous {
public static void main(String[] args) {
System.out.println("We are in main class");
int finalInt = 10;
// Accessed from anonymous class so can not be changed
//finalInt++;
Simple s = new Simple(); // Used as normal class
s.printText();
Simple as = new Simple() { // Used as anonymous class
void printText() {
System.out.println("This is text from anonymous class!!");
anotherMethod();
}
void anotherMethod() {
System.out.println("Final Value : " + finalInt);
System.out.println("Another Method from anonymous class!!");
}
};
as.printText();
// Can not access non-overridden methods from the anonymous class
// as.anotherMethod();
// Accessed from anonymous class so can not be changed
// finalInt++;
}
}
In above class, the class Simple is used in two ways - (1) Create instance of class Simple and (2) Create instance of anonymous class overriding the method from class Simple.
// Accessed from anonymous class so can not be changed
//finalInt++;
// Accessed from anonymous class so can not be changed
// finalInt++;
Above commented statements show that the variables accessed from the anonymous class is treated as final.
// Can not access non-overridden methods from the anonymous class
// as.anotherMethod();
And above statement shows that anonymous class can have more methods which are not overridden, but can not be accessed. Only methods from the class used to declare, can be accessed.
Here is the output:
We are in main class
This is a simple text!!
This is text from anonymous class!!
Final Value : 10
Another Method from anonymous class!!
The code can be accessed here at Github link.
No comments:
Post a Comment