Yesterday, i learned the anonymous constructor in java, not the anonymous class. I haven't seen this constructor before, so i search it in Google. The result is i know how to use it and what it is. But there is little information about this usage.
The anonymous constructor is a block code surround with a pair of braces. And the anonymous will be run before the common constructor and run after the static code block.
I want to know that why nobody use this anonymous constructor. Is there some bad influences to our java application when we use that?
Thanks for any help.
The following is a example of 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");
}
}
/**
* @param args
*/
public static void main(String[] args) {
new Sub();
}
// Results:
// Base static{}static block
// Sub static{}static block
// Base anonymous constructor
// Base() common constructor `enter code here`
// Sub anonymous constructor
// Sub() common constructor
}