An anonymous class is a class declared within the expression. It has no class name and it ends with a semi colon. It starts with new keyword followed by the interface name that you want to implement as an anonymous class. This is just a one time use class. So, this is like a local class with no name and you must directly implement the interface without declaring it since you already have an interface which is also a form of a class but just an abstract class.
========================
class PracticingAnonymousClass {
interface WhatIsYourName{
void getName(String name);
}
protected static void displayName(){
class TestingLocalClass1 implements WhatIsYourName{
@Override
public void getName(String name) {
System.out.println("Your local name is " + name);
}
}
TestingLocalClass1 localClass1 = new TestingLocalClass1();
localClass1.getName("Bell Cranel");
WhatIsYourName anonymousObject = new WhatIsYourName() {
@Override
public void getName(String name) {
System.out.println("Your anonymous name is " + name);
}
};
anonymousObject.getName("Goddess Sama");
}
public static void main(String[] args){
PracticingAnonymousClass.displayName();
}
}===========================Results:Your local name is Bell CranelYour anonymous name is Goddess Sama
The restrictions of anonymous class is the same with the local class.
Rules:
1. It has access to the members(fields and methods) of its enclosing class.
2. To access a local variable (the variable inside the body of the method where the class was declared), that variable must be final or effectively final (without final keyword but you did not change the value as your code progress).
3. If there is a shadowing of variables, you need to implement the rules for shadowing like the this keyword and the Name of the enclosing class.
4. You cannot declare static initializers and member interfaces in an anonymous class.
5. It can have static members provided they are constant variables.
//Testing Rules 4 and 5
======================
class PracticingAnonymousClass {
interface WhatIsYourName{
void getName(String name);
}
protected static void displayName(){
class TestingLocalClass1 implements WhatIsYourName{
@Override
public void getName(String name) {
System.out.println("Your local name is " + name);
}
}
TestingLocalClass1 localClass1 = new TestingLocalClass1();
localClass1.getName("Bell Cranel");
WhatIsYourName anonymousObject = new WhatIsYourName() {
private static String s1;
static {
s1 = "Your anonymous name is ";
}
@Override
public void getName(String name) {
System.out.println(s1 + name);
TryingInterface ti = new TryingInterface() {
@Override
public void displayAnything(String yourText) {
System.out.println(yourText);
}
};
ti.displayAnything("I wonder what to display!!");
}
interface TryingInterface{
void displayAnything(String yourText);
}
};
anonymousObject.getName("Goddess Sama");
}
public static void main(String[] args){
PracticingAnonymousClass.displayName();
}
}===========================Results:Your local name is Bell CranelYour anonymous name is Goddess SamaI wonder what to display!!
I tried rule 4 about static initializers and member interfaces. It works in both. Though, rule 4 and 5 seems to be in a clash. Why? It is because in the example above I used a static initializer to declare a static variable and was initialize in right away in a static initializer. And the value did not change. It is kind of a constant static variable. Though the interface is kind of a different. It is said that member interface will not work within the anonymous class, but in the example above, the interface works just fine. This is to be verified in some reading materials.
YOU ARE READING
Java Programming
Rastgelehttps://docs.oracle.com/javase/tutorial/ This is the link where I learned my java lessons online. I choose java as first programming language. I am a career shifter. I just want to start something new. I choose to write it here in Wattpad so that I...