i understand inner class it's non-static , static method outer class can't reference it.
i have code, doesn't work, , understand why doesn't work.
class outerclass { class innerclass{} public static void outhermethod() { innerclass = new innerclass(); } } but have other code, work, don't understand why it's different first one. why work?
class outerclass { class innerclass{} public static void outhermethod() { innerclass = new outerclass.new innerclass(); } } thanks in advance!
edit: it's not duplicated because isn't same question. i'm not asking static nested classes, i'm asking static methods , inner classes
an inner class requires instance of enclosing class in order instantiated.
a static method of outerclass doesn't have instance of outerclass, can't create instance of innerclass without supplying enclosing instance (of outerclass).
innerclass = new innerclass(); would work within instance method of outerclass.
innerclass = new outerclass().new innerclass(); works within static method since creating instance of outerclass , use enclosing instance instance of innerclass.
Comments
Post a Comment