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

  • Home
  • SEARCH
  • 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 6222147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:15:47+00:00 2026-05-24T08:15:47+00:00

I have a general idea of how the Garbage Collector works in Java, but

  • 0

I have a general idea of how the Garbage Collector works in Java, but my reasoning for destroying an object is not because I care about freeing up memory, but because of functionality. I can explain better with an example:

Say I’m making a game where money is involved. When a person picks up a Money object off the ground, I want to call that object’s addTo method, which involves adding a value to that person’s wallet.

public class Money {

  /** The value of this object. */
  private final int value;

  // constructor
  public Money(double x) {
    this.value = x;
  }

  // Should be called when a Person picks up this object.
  public void addTo(Person bob) {
    bob.addToWallet(this.value);
  }

  // MAIN METHOD
  public static void main(String[] args) {
    Person bob = new Person();
    Money dollar = new Money(1.00);
    dollar.addTo(bob);    // bob finds $1.00
  }
}

After dollar is found, I don’t want someone else to be able to pick it up. In other words, I don’t want myself or any other program to be able to accidentally call the line:

dollar.addTo(alice);

So after Bob picks up the money, its value is added to his wallet and there’s no need for an object representation of that value anymore. It’s not that I care about the memory the object is using, but I don’t want that object to be accidentally used somewhere else in the program. How do I destroy dollar besides setting dollar = null;?

  • 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-05-24T08:15:48+00:00Added an answer on May 24, 2026 at 8:15 am

    You shouldn’t be thinking of garbage collection at all. It is up to the JVM to determine when things can be garbage collected and 99% of the time, you won’t need to consider this at all if you program responsibly.

    Answer me this .. how would “the object be accidentally used somewhere else in the program”?

    The answer is, it couldn’t. A local reference exists until the end of your main method and no longer. Unless you store a reference to it, it cannot be referenced.

    As your program grows, it might be a good idea to store a reference to it temporarily. You need to represent your concept of “the ground” with an object. Since the functionality you describe is basic, you can just use any of the built in implementations of Collection for example List.

    List<Money> floorMoney = new LinkedList<Money>();
    floorMoney.add(new Money(1.00));
    

    and when you associate money from the floor with a Person you would remove it from the floor.

    Money dollar = floorMoney.get(0); // how you access elements may vary
    floorMoney.remove(dollar);
    dollar.addTo(bob);
    

    Now, unless you store a reference to dollar somewhere in your addTo implementation, the Money object will be abandoned and later GC’d.

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

Sidebar

Related Questions

I'm not entirely familiar with how CLI works, but I have a general idea.
I have the general idea of how to do this in Java, but I
I have a general idea on how NSAutorelease pool works. we have objects in
I have a general question about the way that database indexing works, particularly in
I have a general idea of what an AST is, but I want to
I read about creating user groups, I think I have the general idea. For
I have a general idea, and there are some obvious cases, but there are
I'm a newcomer to using C++ but have got a general Idea of its
I have a general question about how .dll/.libs are suppose to be used. I
I have a general question for the group. I am about to start a

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.