Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes class instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "class instance" Watch "class instance" New topic
Author

class instance

souha jerbi
Greenhorn

Joined: Apr 13, 2009
Posts: 15
hi,
how may i know if a class is instanciated or not?
Robert Sand
Greenhorn

Joined: May 08, 2009
Posts: 2
At least one opportunity is to make a static variable, and increase it by one in the constructor.. then you know how many times it has been intanciated..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
Welcome to JavaRanch

That would work, but will not tell you whether any objects have been destroyed.

But part of object-oriented programming in a high-level language is that you don't need to record whether a class has been instantiated or not.
Robert Sand
Greenhorn

Joined: May 08, 2009
Posts: 2
Thank you

I just wonder though, if you make a static boolean set to false, that you change to true in the constructor, would it be reset when all the objects have been destroyed?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
Only if you reset it in the finalize() method. But that will produce all sorts of unexpected effects, and we don't recommend calling finalize if you can help it.You can try it, and you see that all instances appear not to exist after an instance has been deleted by the garbage collector, but they all reappear when you instantiate a new object!

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3947
    
  17

And just for fun, to combine the 'count instances' approach, and the 'use a finalizer' approach, maybe this could work:


Again, just for fun, don't rely on finalizers, count won't be accurate, and don't depend on GC warnings, etc...

The real question though, is why do you want to know when an Object has been created / if they exist? Maybe there is a better and more reliable solution.


Steve
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Perhaps a static WeakHashMap, or list of WeakReference object would help.

A WeakHashMap would help in automatically clearing the references that have been garbage collected, but it's still a hash map - it can't store duplicate (according to equals) keys. A list of WeakReference objects does not have that problem, but it does not remove any references automatically - the get() method will simply return null.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3947
    
  17

Rob Prime wrote:Perhaps a static WeakHashMap, or list of WeakReference object would help.

A WeakHashMap would help in automatically clearing the references that have been garbage collected, but it's still a hash map - it can't store duplicate (according to equals) keys. A list of WeakReference objects does not have that problem, but it does not remove any references automatically - the get() method will simply return null.


That was what I was thinking - if the tracking was needed then using a list of WeakReferences or PhantomReferences and a ReferenceQueue could be used to more accurately track the count of active referenced instances.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
I presume this is all being asked for learning or entertainment. Would anybody need this information in real life?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: class instance
 
Similar Threads
can non-static inner class access any member
andrew has wii
Doubt about diagram class
Doubt about diagram class (realization)
WA #1.....word association