• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

inner anonymous class return object

 
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
reply
    Bookmark Topic Watch Topic
  • New Topic