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 9095501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:37:01+00:00 2026-06-16T23:37:01+00:00

Possible Duplicate: Does setting Java objects to null do anything anymore? I am using

  • 0

Possible Duplicate:
Does setting Java objects to null do anything anymore?

I am using a same variable again and again in a method & referring it to a new object at many a times.. Is it a good practice from garbage collection aspect to nullify it before making it refer a new object.

Example:

StopWatch watch = new StopWatch();
watch.start();
//some code
watch.stop();
//some code
watch = null;
watch = new StopWatch();
watch.start();
//some code
watch.stop();
//some code

Not sure whether nullifying it make a difference to GC in this case. Please guide.

Thanks!

  • 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-16T23:37:03+00:00Added an answer on June 16, 2026 at 11:37 pm

    Assigning null at that point will make no difference, because you are immediately going to assign a new value to that variable.

    There is nothing “magical” in Java about assigning null to a variable. It doesn’t cause the object to be garbage collected immediately. All it is doing is breaking one of (possibly) many “paths” by which the object in question is reachable. If the path would break / disappear of its own accord before the next GC run, then assigning null achieves nothing.

    Normally it is not worth nulling variables or fields in Java, and it is certainly not worth doing for a local variable is about to be overwritten, or is about to go out of scope.

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

Sidebar

Related Questions

Possible Duplicate: PHP Using a variable when calling a static method I've read through
Possible Duplicate: What does :_* (colon underscore star) do in Scala? I’m using the
Possible Duplicate: Does Dispose method still get called when Exception is thrown inside of
Possible Duplicate: Does std::list::remove method call destructor of each removed element? I have a
Possible Duplicate: Does an open-ended interval implementation exist for Java? i have an int
Possible Duplicate: In PHP, what is the differences between NULL and setting a string
Possible Duplicate: Does using references instead of pointers, resolve memory leaks in C++? When
Possible Duplicate: Does Java need tuples? Does Java support triples or at least pairs?
Possible Duplicate: Java command line arguments. Using * as an argument for multiplication i
Possible Duplicate: Is setting the uploads folder 777 permision secure? I am using WordPress

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.