Java inner class and static nested class -


what main difference between inner class , static nested class in java? design / implementation play role in choosing 1 of these?

from java tutorial:

nested classes divided 2 categories: static , non-static. nested classes declared static called static nested classes. non-static nested classes called inner classes.

static nested classes accessed using enclosing class name:

outerclass.staticnestedclass 

for example, create object static nested class, use syntax:

outerclass.staticnestedclass nestedobject = new outerclass.staticnestedclass(); 

objects instances of inner class exist within instance of outer class. consider following classes:

class outerclass {     ...     class innerclass {         ...     } } 

an instance of innerclass can exist within instance of outerclass , has direct access methods , fields of enclosing instance.

to instantiate inner class, must first instantiate outer class. then, create inner object within outer object syntax:

outerclass.innerclass innerobject = outerobject.new innerclass(); 

see: java tutorial - nested classes

for completeness note there such thing inner class without enclosing instance:

class {   int t() { return 1; }   static a =  new a() { int t() { return 2; } }; } 

here, new a() { ... } inner class defined in static context , not have enclosing instance.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -