Results 1 to 2 of 2
Thread: Returning local objects
- 12-07-2012, 10:33 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 21
- Rep Power
- 0
Returning local objects
I have a class that has 4 lists. I want a method that will return one list that is the "sum" of the 4 lists. The way I envision this is:
In C++, this is bad news, the newList is local to the function, and goes out of scope upon return. Is this true with Java as well?Java Code:public List<String> getLists() { List<String> newList = new List<String>(); // Add my first list to newList // Add my second list to newList // Add my third list to newList // Add my fourth list to newList return newList; }
If so, I should mention that I have made the List class myself, thus I could do some constructor work:
Any better? ThanksJava Code:public List<String> getLists() { List<String> newList = new List<String>(); // Add my first list to newList // Add my second list to newList // Add my third list to newList // Add my fourth list to newList return new List<String>(newList.begin(), newList.end()); // Return a copy of the local list }
- 12-11-2012, 01:14 AM #2
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: Returning local objects
Hell no, even in C++ you may do it like that if you use pointers of course - only the variable is local, the object remains if created correctly. The point is that this will work fine as java will keep any object as long as it is referenced somewhere.
Usually you add other lists by using newList.addAll(...) - note however that with that you do not create "New" objects. Every change you do in the old lists will be in the new lists as the objects contained will be the exact same.I like likes!.gif)
Similar Threads
-
Noob question - Create objects using objects as parameters
By pantaloc in forum New To JavaReplies: 12Last Post: 04-29-2012, 02:55 PM -
Function call returning array of objects
By ShitalJain in forum New To JavaReplies: 4Last Post: 06-25-2011, 09:39 AM -
logic for Uploading all the local files from local machine to remote server using FTP
By agangaia in forum New To JavaReplies: 1Last Post: 03-05-2011, 05:47 PM -
Polygons... returning and passing objects?
By asherwolf in forum New To JavaReplies: 3Last Post: 07-09-2010, 04:41 PM -
read txt file,with some records, create objects and store objects in tables of a db.
By stamv in forum JDBCReplies: 1Last Post: 01-22-2009, 04:25 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks