4

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

}    
William
  • 593
  • 1
  • 4
  • 7
  • 4
    There no such thing as an "anonymous constructor". It's an **instance initializer**. Possible duplicate of [How is an instance initializer different from a constructor?](http://stackoverflow.com/questions/1355810/how-is-an-instance-initializer-different-from-a-constructor) – Brian Roach Dec 14 '13 at 08:14
  • This is a sample to show how the anonymous works and the time of anonymous constructor be called. Not a complete application. These code can run good. – William Dec 14 '13 at 08:16
  • Again ... no such thing. Please see duplicate question. – Brian Roach Dec 14 '13 at 08:17
  • I think people do not really know how to use it. – Anders Lindén Dec 14 '13 at 08:17
  • How can a question about programmers preferences be a duplicate of a question about differences between program language constructs? – Anders Lindén Dec 14 '13 at 08:18

4 Answers4

1

The construct is called an instance initializer. I suspect it is seldom used because most initialization can be performed in field initializers or in constructors.

I did use it once, though, and it came in handy. I had a field that was initialized to something, and I wanted to change it to check a couple properties before assigning a default value. I could have written that code in the constructor, but the constructor was at the other end of the file. Putting an instance initializer right next to the place where the field was declared and documented was useful and convenient.

Stuart Marks
  • 127,867
  • 37
  • 205
  • 259
0

Continuing along the lines of speculation, there is from time to time a pre and post constructor call desire, especially at times where you aren't in full control of how the hierarchy is implemented.

Say, for example you DIDN'T have source for Base, but wanted to initialize some instance variables prior to its constructor running. It's a fringe case but it does come up.

I agree with the other poster it's a counter intuitive approach and one might even say has a hackish feel, but it could theoretically solve the example case.

Malachi
  • 2,260
  • 3
  • 27
  • 40
0

1) I think it's not used probably because

anonymous constructor {
   do A
}

regular constructor {
   do B
}

is basically the same as

regular constructor {
    do A
    do B
}

and the latter is more explicit and clear.

2) Most people don't really know about it.
I myself have totally forgotten it exists.
But now I recalled that I had read about it
once in a SCJP book but that was long ago.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
0

Rare usage of this construct is for reasons of code style

I guess the main reason why most people dislike these instance intializer blocks, is the fact, that the instance initialization gets fragmented in your source code. E.g., most people would look for field initialization directly at the field declaration or at the constructor body.

The double-brace notation

However, there is a use of instance initializer blocks I stumble over from time to time. Consider the following expression: new java.util.HashSet<String>() {{ add("foo"); add("bar"); }}. This one-liner creates a new HashSet with the initial String values "foo" and "bar" inserted. The construct used here is sometimes referred to as the "double-brace-notation", even though it's not an own construct, but the mixture of two constructs: The outer brace creates an anonymous subclass of HashSet and the inner brace is an instance initializer for this subclass. In such a construct, where no methods are overriden, it's safe to call overridable methods in an instance initializer block.

Static initializer blocks

Even though it's not the same construct, it's noteworthy, that there exists the same thing for static class initialization: static { }. This thing seems to be more common, since there is no "constructor" for class initialization.

Felix Feisst
  • 175
  • 10