• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

class instance

 
souha jerbi
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
how may i know if a class is instanciated or not?
 
Robert Sand
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume this is all being asked for learning or entertainment. Would anybody need this information in real life?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic