Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8529501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:02:50+00:00 2026-06-11T09:02:50+00:00

In terms of garbage collection within Java (any version) are these two code blocks

  • 0

In terms of garbage collection within Java (any version) are these two code blocks equivalent as to when they will become eligible:

public class Foo  
{  

    public Foo()  
    {  
        go();   
    }  

   public List go()  
    {  
       List things = new List();  
        things.add(42);  
        things.add(new Object());
        return things;
    }  
} 

and the following:

public class Foo  
    {   
        public Foo()  
        {     
        }  

        public List go()  
        {  
           List things = new List();  
            things.add(42);  
            things.add(new Object());
            return things;
        }  
    } 

The question I have is the following:

Does a function invoked from a constructor cause the garbage collector to ignore any objects that are scoped within that function if they were invoked from a constructor?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:02:51+00:00Added an answer on June 11, 2026 at 9:02 am

    In your first class go() is called from the constructor. Until the line of code : go() is called, nothing exists for that object but a space in memory pointed to by the variable that the object was instantiated to. At the time go() is called, a new list is created in memory, things are added and the list is returned. Unless that list returned was saved to a variable somewhere it is garbage. The method remains as an instance method of the object but the returned list object is now gone.
    Your second example does the same thing as the first one except the constructor is not calling the method go(). The method still exists as an instance method of the object, but the list object was never created.
    So once the first example exits the method go(), the first example is in the same state of rest as the second example would be upon creation. Both ready to create and return a list with their own method, but neither having a list or memory for one allocated yet. It is up to the garbage collector WHEN the list in the first example will be actually collected, but it has been marked as garbage (unless saved to a variable somewhere in code not seen in the examples).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm curious to know if either of these two Java method invocations will behave
From what I understand REE is better in terms of garbage collection and much
I'm having a hard time dealing with a Java garbage collection problem, and interpreting
In terms of actual low level atomic instructions and memory fences (I assume they're
In general terms and pseudo-code, what would be the best way to have a
I see these terms used interchangeably as the global environment for the DOM. What
In terms of: Speed Required processing (which will influence speed) Following standards Which of
These terms are used in the Definition of a simple interpretation table on http://www.w3.org/TR/2004/REC-rdf-mt-20040210/#interp
I have been reading about garbage collection and came to know the term Island
In terms of data accessing, or any other opinions. If possible, state a better

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.