• Post Reply Bookmark Topic Watch Topic
  • New Topic

inner anonymous class return object  RSS feed

 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In above coding, how could return oject to the outer class ? for example, "outerC" should be get the collection "students" .

Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can assign directly to the variable "outerC" if it's a member variable of the enclosing instance. If it's a local variable, then you need to do a trick of some kind, because inner classes can only access final local variables. One thing you could do would be to initialize outerC like this:\

final Collection outerC = new ArrayList();

and then in the inner class, say

Collection students = DBFunctions.loadData(pb,productId,null,null);
outerC.addAll(students);
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
You can assign directly to the variable "outerC" if it's a member variable of the enclosing instance. If it's a local variable, then you need to do a trick of some kind, because inner classes can only access final local variables. One thing you could do would be to initialize outerC like this:\

final Collection outerC = new ArrayList();

and then in the inner class, say

Collection students = DBFunctions.loadData(pb,productId,null,null);
outerC.addAll(students);


In my humble opinion, such a solution is a dirty hack. I'd first like to know more about *why* you want to "return" the value, to evaluate better alternatives...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!