java - Can a Static Nested Class be Instantiated Multiple Times? -
given know of every other type of static feature of programming––i think answer 'no'. however, seeing statements outerclass.staticnestedclass nestedobject = new outerclass.staticnestedclass(); makes me wonder.
yes, there nothing in semantics of static nested type stop doing that. snippet runs fine.
public class multiplenested { static class nested { } public static void main(string[] args) { (int = 0; < 100; i++) { new nested(); } } } see also
public static interface map.entry<k,v>public static class abstractmap.simpleentry<k,v>- probably well-known nested type. instantiated multiple times.
now, of course nested type can own instance control (e.g. private constructors, singleton pattern, etc) has nothing fact it's nested type. also, if nested type static enum, of course can't instantiate @ all.
but in general, yes, static nested type can instantiated multiple times.
note technically, static nested type not "inner" type.
jls 8.1.3 inner classes , enclosing instances
an inner class nested class not explicitly or implicitly declared
static.
that is, according jls terminology, inner class 1 isn't static. if it's static, it's nested type.
so static mean?
static means nested type not need instance of enclosing type instantiated.
Comments
Post a Comment