Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
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
?
258 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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Starting up a Project

564 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