Results 1 to 3 of 3
  1. #1
    petzeee is offline Member
    Join Date
    Jan 2013
    Rep Power

    Default Lifecycle of objects created inside a function?


    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;
    And let's say that this function is called like this:
    Java Code:
    Dog dog = myCustomFunction();
    So, the variable dog will have a reference to the object in the memory, even after the function returns, right? What if the Object Dog has, apart from its name, other "fields" which are objects itself? Their reference will still be intact, an not destroyed by the garbage collector?

    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.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Washington, DC
    Rep Power

    Default 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!

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default 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

  1. How to find the objects are created in heap only?
    By akiravelmont in forum New To Java
    Replies: 1
    Last Post: 12-07-2012, 07:47 PM
  2. Create a function that sorts an arraylist of objects
    By Stavrosgr in forum New To Java
    Replies: 2
    Last Post: 12-03-2012, 07:33 PM
  3. recursive function to create many objects?
    By dacoolest in forum New To Java
    Replies: 1
    Last Post: 11-22-2011, 09:03 PM
  4. Replies: 3
    Last Post: 09-29-2010, 03:38 PM
  5. Replies: 0
    Last Post: 08-14-2010, 12:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts