constructor - Java Object Construction and Enclosing Method -


i've been looking book or document better explain what's going on here:

system.out.println(">constructor>" + new object().getclass().getenclosingconstructor()); 

output:: >constructor >null

however,

system.out.println(">constructor>" + new object(){/**/}.getclass().getenclosingconstructor().tostring()); 

output:: >constructor >packageinformation.classconstructor()

i know has ... ... static initialization sequence.

my question is: how work , can read more this?

thanks.

you can read in javadoc class.getenclosingconstructor:

if class object represents local or anonymous class within constructor, returns constructor object representing enclosing constructor of underlying class. returns null otherwise.

since object isn't anonymus class, null returned in first case.

new object(){/**/} creates instance of anonymus class extending object, why non-null value returned, if new object(){/**/}.getclass().getenclosingconstructor() placed inside constructor.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -