Yesterday I found out an anonymous constructor in java, not an anonymous class. I haven't seen this constructor yet, so I'm looking for it on Google. As a result, I know how to use it and what it is. But there is little information about this use.
An anonymous constructor is a code block environment using a pair of curly braces. And anonymous will be run before the general constructor and run after the static block of code.
I want to know why no one is using this anonymous constructor. Are there any bad effects on our Java application when we use it?
Thanks for any help.
The following is an example of an anonymous constructor:
public class Static_Super_Conustruct {
static class Base{
{
System.out.println("Base anonymous constructor");
}
public Base() {
System.out.println("Base() common constructor");
}
static{
System.out.println("Base static{} static block");
}
}
static class Sub extends Base{
{
System.out.println("Sub anonymous constructor");
}
public Sub() {
System.out.println("Sub() common constructor");
}
static{
System.out.println("Sub static{} static block");
}
}
public static void main(String[] args) {
new Sub();
}
}
source
share