5 Replies - 180 Views - Last Post: 21 January 2013 - 02:07 PM Rate Topic: -----

#1 Dice93  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 39
  • Joined: 24-December 12

How do you remove objects in java

Posted 21 January 2013 - 01:46 PM

So example, we've declared an object,
Class object = new Class();
so how do you remove it ?
object = null;
doesn't help..
Is This A Good Question/Topic? 0
  • +

Replies To: How do you remove objects in java

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,596
  • Joined: 27-December 08

Re: How do you remove objects in java

Posted 21 January 2013 - 01:49 PM

Objects are garbage collected in Java. Removing references to an object by setting variables = null will allow the GC to remove the objects sooner.
Was This Post Helpful? 1
  • +
  • -

#3 Dice93  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 39
  • Joined: 24-December 12

Re: How do you remove objects in java

Posted 21 January 2013 - 01:53 PM

Thanks for reply.
So when we call object = null, so there's delay for removing object right?
I don't have to call GC right ?
Was This Post Helpful? 0
  • +
  • -

#4 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: How do you remove objects in java

Posted 21 January 2013 - 01:59 PM

No, you don't have to call the GC. objects are automatically garbage collected when there are no reachable references left.

This means that if you dont have to specify
object = null;
, because once the object goes out of scope it will be GC'd

This post has been edited by ianian112: 21 January 2013 - 02:00 PM

Was This Post Helpful? 2
  • +
  • -

#5 Dice93  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 39
  • Joined: 24-December 12

Re: How do you remove objects in java

Posted 21 January 2013 - 02:06 PM

I got an error

Quote

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,596
  • Joined: 27-December 08

Re: How do you remove objects in java

Posted 21 January 2013 - 02:07 PM

If you try to use a variable that has a null reference, you will get that Exception.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1