So in the class:
public class Test {
private Short age = 34;
public void reset() {
age = 0;
}
}
when will the object pointed to initially by age be garbage
collected?
Kind regards from
Heinz
--
Dr. Heinz M. Kabutz (Maximum Solutions)
Sun Java Champion
Author of "The Java(tm) Specialists' Newsletter"
<http://www.javaspecialists.co.za/>
Tel: +27 (83) 340-5633
> -----Original Message-----
> From: [email protected]
> [mailto:[EMAIL PROTECTED] Behalf Of Michael Wiles
> Sent: 09 March 2006 17:21
> To: CTJUG Forum
> Subject: [CTJUG Forum] Re: Silly Question about Collections
> and Pointers
>
>
>
> In java you can't really "destroy" an object. When you say
> destroy, what are you actually doing? Are you simple going
> objectReference = null; This will not destroy the object. It is merely
> removing a reference to the object (not all the references though).
>
> It is the Garbage collector's job to destroy an object i.e. reclaim
> the memory taken by that object. You cannot force the removal of an
> object. All you do in java is remove all the references to an object.
> That's as much as you can do. In your case the garbage collector will
> not do anything to the object because it detects that there is still a
> reference to it (held in the array).
>
> The other thing is when you say "recreate it" - technically
> speaking you can't really recreate an object, once an object is
> created and destroyed/garbage collected, it cannot be recreated. When
> someone says "recreate the object", what they're actually saying
> is create another instance of the class that the first object was
> created from. It's not the same instance, but a new instance.
>
> It's the same in any object oriented language. You create
> "instances" of classes, these are objects. Often in documentation,
> class and object are used interchangeably so it can be confusing. Try
> and think of the class as the template/definition and an object as the
> realization of that template.
>
> You should also realize that the reference to an object is a value in
> itself. It is something separate from the object to which it
> points. It
> can point to a different object if you so desire.
>
> In java you don't put the actual objects anywhere, you just put the
> _reference_ to the object somewhere. In other words, when you push an
> object to an array like you say, you are merely pushing a reference to
> an object onto the array. It does not make a copy of the object. The
> only way to make a copy is to explicity ask it to.
>
> However, be aware that for some particular data types in java this
> reference --> object does not hold. int, byte, long, double, float,
> char and boolean are what are known as "primitive" types for which
> there is no reference --> object differentiation.
>
> Hope this helps...
>
> Michael
>
>
>
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CTJUG Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/CTJUG-Forum
-~----------~----~----~----~------~----~------~--~---