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
classobject represents local or anonymous class within constructor, returnsconstructorobject representing enclosing constructor of underlying class. returnsnullotherwise.
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
Post a Comment