Results 1 to 3 of 3
  1. #1
    sagngh8 is offline Member
    Join Date
    Nov 2010
    Rep Power

    Default Assigning null to Complex Type object in the context of memory in java

    What is the meaning of assigning null to Complex Type object in the context of memory in java language.

    while(true) //Approx 9000 times

    Employee objEmployee = new Employee ();

    objEmployee = //Put some value

    objEmployee = null;

    Does assigning null to the complex type make the object ready for garbage collection in java.Also
    is it the way to efficient memory management in java.


  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Rep Power


    If that is the only reference to the object, then yes, it makes it GC "ready". Is it any better than simply letting the Object go out of scope? In the above example, no, in the case of a long-lived instance variable or something, maybe, as long as you are not simply immediately reassigning a new value to the instance variable (in which case you can skip the null assignment, of course) and are not doing it shortly before the "end" of the instance (in which case you can simply let it go out of scope, again).

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


    What masijade says, but I'll add that this is (outside of some very specific cases) usually a sign you've written something incorrectly.
    You should (outside of etc etc) never need to do this, and I use it (in code reviews) as a flag that the author has gone wrong somewhere.

Similar Threads

  1. Replies: 8
    Last Post: 03-04-2011, 02:50 PM
  2. FAIL - Invalid context path null was specified
    By humanbeing in forum Java Servlet
    Replies: 0
    Last Post: 08-13-2010, 12:14 PM
  3. Assigning values to an object
    By camper2 in forum New To Java
    Replies: 4
    Last Post: 04-05-2009, 04:13 AM
  4. using instanceof to get Object type and parent type?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 11-06-2008, 07:24 PM
  5. Replies: 1
    Last Post: 08-07-2007, 12:10 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