File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes inner anonymous class return object Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "inner anonymous class return object" Watch "inner anonymous class return object" New topic
Author

inner anonymous class return object

Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 758


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
Marshal

Joined: Jul 08, 2003
Posts: 24057
    
  13

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);


[Jess in Action][AskingGoodQuestions]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: inner anonymous class return object
 
Similar Threads
list empty using s:select tag with Struts 2
static inner classes
Concurrent Modification Exception ?
How To Manually Set JavaBean Values to Create a Collection?
How to make my class iterable?