java - Static nested class instance -
this question has answer here:
- java inner class , static nested class 23 answers
i came across in java, know static nested class why create instance of 'static'. isn't whole idea of 'static' use without instance? understand concept of inner classes, why (and frankly how possible to) create 'instance' of 'static' member @ ?
why this:
1- outerclass.staticnestedclass nestedobject = new outerclass.staticnestedclass(); 2- nestedobject.anestedclassmethod();
and not this:
1- outerclass outerinstance=new outerclass(); 2- outerinstance.staticnestedclass.anestedclassmethod();
use on inner classes, keyword static indicates can access inner class without instance of outer class.
for example:
public class outer { public static class inner { /* code here. */ } }
with construction, can create instance of inner class using code:
new outer.inner()
if inner class not static, this:
public class outer { public class inner { /* code here. */ } }
then, have create instance of outer in order access inner:
new outer().new inner()
Comments
Post a Comment