Results 1 to 3 of 3
- 01-24-2013, 08:57 PM #1
Member
- Join Date
- Jan 2013
- Posts
- 1
- Rep Power
- 0
Lifecycle of objects created inside a function?
Hi,
We all know that JAVA variables in a function are only valid until this specific function returns. But what about objects which are created inside a function? Let's say like this:
Java Code:public static Dog myCustomFunction(){ Dog myDog = new Dog("Jake"); return myDog; }
Java Code:Dog dog = myCustomFunction();
I'm asking this because I have a problem with nested objects. All of a sudden, some parts are randomly overwritten with different objects/values. I always thought that a memory space allocated to an object wouldn't be "destroyed" by the garbage collector as long as there is a reference pointing at it.
- 01-24-2013, 09:25 PM #2
Re: Lifecycle of objects created inside a function?
You're right. As long as a reference to an Object exists, it won't be garbage collected. Even if it was, it wouldn't really make sense that you'd have different Objects in its place. Nulls maybe, but not just random different data. There must be something wrong with your code/logic. Time to break out the debugger!
How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 01-25-2013, 10:22 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: Lifecycle of objects created inside a function?
Sounds to me like you have a case of shared references.
So you have assigned a reference to object X to an attribute inside object Y.
You then also assign the same reference to object Z later on.
Object Z changes a value in X. Since Y has the same reference it sees that change.Please do not ask for code as refusal often offends.
** This space for rent **
Similar Threads
-
How to find the objects are created in heap only?
By akiravelmont in forum New To JavaReplies: 1Last Post: 12-07-2012, 07:47 PM -
Create a function that sorts an arraylist of objects
By Stavrosgr in forum New To JavaReplies: 2Last Post: 12-03-2012, 07:33 PM -
recursive function to create many objects?
By dacoolest in forum New To JavaReplies: 1Last Post: 11-22-2011, 09:03 PM -
how to use objects and its values to use in other function without using parameters
By sfaiz in forum New To JavaReplies: 3Last Post: 09-29-2010, 03:38 PM -
How do I access objects in a vector which is inside a java bean in struts?
By skaterdav85 in forum Web FrameworksReplies: 0Last Post: 08-14-2010, 12:20 AM
Bookmarks