Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can we count the number of objects created per class using Java1.5 API?

 
Jalli Venkat
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How can we count the number of objects created for a class using Java1.5 API..?
Is there any such API available...?
If not means , what is the solution for this....?

 
Paul Clapham
Sheriff
Posts: 21298
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
fred rosenberger
lowercase baba
Bartender
Posts: 12180
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that while this will tell you how many have been CREATED, that does not tell you how many are currently ALIVE. You could easily write a for loop that creates 100 objects and immediately discards the reference, making them all eligible for garbage collection. And you have no way to know when that may happen.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The whole idea of using a high-level language is to avoid having to bother about such questions.
 
Sidharth Pallai
Ranch Hand
Posts: 134
Hibernate Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


How to judge which objects are eligible for garbage collection. This code extract has been taken from Johnathan Giles java note. The explantion given by him is confusing me. Here it is ....
Even when e3 and e2 are set to be null, it is
not possible to do any garbage collection,
even though it feels like it should be possible
to garbage collect e2.
This is because e2 is still accessible from e1,
indirectly. E1.e refers to the original e3, and
e3.e still refers to the original e2.
Therefore, no garbage collection is possible
until all three objects are set to null, and they
}
}
become an island of references ready for GC.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a question unrelated to the original thread. Please post that as a new thread; asking unrelated questions is called hi-jacking and can deprive the original poster of contact with their thread.
 
salvin francis
Bartender
Posts: 1305
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with campbell on this:

Campbell Ritchie wrote:The whole idea of using a high-level language is to avoid having to bother about such questions.



However if you do want such functionality .....
I suggest you use a static variable that is incremented over every Object creation.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just had a question about this in my class. I think the cleanest answer is:



My $0.02,
Janeice
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janeice DelVecchio wrote:I just had a question about this in my class. I think the cleanest answer is:


Is it thread safe?
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know what thread safe means, but I don't know how to tell if something IS thread safe.............
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Concurrency in Action, Janeice, by Brian Goetz and several other people. It's reviewed on our book reviews pages.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I think you have the ++ operator in the wrong place, Janeice. And surely the second line belongs inside a constructor??
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And I think you have the ++ operator in the wrong place, Janeice. And surely the second line belongs inside a constructor??


The ++ operator is in the correct place. I stand by that. Otherwise, the very first instance would be 0, not 1.

Although I suppose the second line could go in a constructor, if you are only using the default constructor, is it necessary?

I'll check out that book, too.

-Janeice
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So my guess is that it would be threadsafe if the two lines were inside a "synchronized" constructor:



Better? I'm not really sure about this....

I think the problem with my class is the bar is set pretty low. They care that things compile and work correctly. No one talks about more advanced things. With some of the responses I've gotten from the graders, I bet not many people put much effort into their projects.
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janeice DelVecchio wrote:Better? I'm not really sure about this....


This is a threadsafe version of the class. You can't synchronize a constructor so instead a static creation method is introduced. The actual increment can alternatively be made in the private constructor. Many would prefer that for reasons of clarity.



Now objects can be created from any thread and you don't risk having two objects with the same number.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so when you make a widget, you do this?



?
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janeice DelVecchio wrote:so when you make a widget, you do this?



?


No you use it like an ordinary static method like this,

Widget foo = Widget.next();

Note that a creation method usually is called "create" or has "create" in the name, so maybe "createNext" would've been a better choise.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . or "getInstance()"
 
Paul Clapham
Sheriff
Posts: 21298
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: . . . or "getInstance()"


But "getInstance" suggests you're going to be getting an instance which already exists, as in a singleton class. If you look through the Java API documentation, there's a large number of methods named getInstance, but they are all (I think) static members of their classes. That's the static factory pattern. Methods which genuinely create a new instance tend to be called newInstance, and there's about 50 of those in the Java API. Some of those are static members, too, but quite a few are not.

There aren't any "createInstance" methods in the API, but there's a long list of "createThis" and "createThat". So a method which creates a Widget could be called "createWidget".
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Point taken, Paul C.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmmmm..... this is so close to that Singleton pattern it's scary. I'm sad that I didn't see it sooner.

Really, the only difference is that the static method is not caring whether or not an instance already exists... it just makes one (and in our version counts the instances).

Thanks! I learn so much around here!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic