?
Solved

Java:  how do I create a class that can keep track of how many times it has been instanciated?

Posted on 2005-04-02
11
Medium Priority
?
251 Views
Last Modified: 2010-04-17
Hello experts,

First off, I am new to java and familiar with C++.. What I would like to do is create a class that instantiates each object with a unique positive ID.  This is a "toy" problem.  That is, I do not need any fancy identifiers - a simple integer that increments for each object created will do.. The trick is, how do I make the class remember how many objects have been initiallized so that I can simply assign the next free integer ID to a new object?

Thank you expterts!

_TH
0
Comment
Question by:travishaberman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 7

Expert Comment

by:lhankins
ID: 13690033


public class Foo
{
   private static int instanceCount;

   public Foo()
   {
      incrementNumInstances();
   }

   protected synchronized static void incrementNumInstances()
   {
      instanceCount++;
   }

   public static int getInstanceCount()
   {
      return instanceCount;
   }
}
0
 
LVL 7

Expert Comment

by:lhankins
ID: 13690040
explanation of my snippet above:

1) declare a static member variable called "instanceCount" that counts the number of instances of the class.
2) define a static synchronized method called incrementNumInstances() that increments the instance count.  This is synchronized, because more than one thread may be instantiating Foo objects simultaneously
3) call the aforementioned increment method during the constructor of the Foo class
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 2000 total points
ID: 13690041
Just have to create a static member and increment it in the constructor...
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

Author Comment

by:travishaberman
ID: 13690074
So "static"  will insure the variable is not reinitialized every time I create a new object?
0
 

Author Comment

by:travishaberman
ID: 13690087
where is the instanceCount initialized in the first place?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13690096
public class Foo
{
   static int instanceCount = 0;

...etcetera....
0
 

Author Comment

by:travishaberman
ID: 13690134
You are a life saver my friend!!  thank you much .. that worked perfectly..

-TH
0
 

Author Comment

by:travishaberman
ID: 13690140
OOps... I am very sorry olivares.. I should have given you credit as well.. I thought it was one person posting to me..  I have another post here.. that I will give you credit for if you full credit for if you drop me a simple answer..

http://www.experts-exchange.com/Programming/Q_21374302.html

its worth 500.. again.. sorry I didn't split some points.. but go there and I will toss um your way ..

thank you ..

-Th
0
 

Author Comment

by:travishaberman
ID: 13690143
oops again.. I meat that last post to be for..

lhankins
0
 
LVL 7

Expert Comment

by:lhankins
ID: 13690236
No problem, mistakes happen... I'm sure the admins could fix it.  For now, I'll just post a comment in the other thread.
0
 
LVL 7

Expert Comment

by:lhankins
ID: 13690261
the other thread is already closed...
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Simple Linear Regression
Six Sigma Control Plans
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question