class - Java. Method Inner Object as return type -
can return method local inner object method?
public class myclass { public mymethodinnerclass getmethodinnerclassobject() { class mymethodinnerclass { } mymethodinnerclass mymethclass = new mymethodinnerclass(); return mymethclass; } }
throws compilation error. if can't return method-local inner class object, how can save after method returns? how can reference object future usage?
exception thrown:
exception in thread "main" java.lang.error: unresolved compilation problem: methodinnerclass cannot resolved type
and also, i'm aware, local variables in method stored in stack , deleted after method exists.
the scope of class inside method only. can however
public object getmethodinnerclassobject() {
or
static class mymethodinnerclass { } public mymethodinnerclass getmethodinnerclassobject() { return new mymethodinnerclass(); }
Comments
Post a Comment