File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes count for instance of a class 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 "count for instance of a class " Watch "count for instance of a class " New topic
Author

count for instance of a class

Raja Sekhar
Greenhorn

Joined: Jul 11, 2003
Posts: 2
HI friends can any one tell me that
Think so that I have one class like this.
Class myclass ()
{
int I =10;
public mymethod()
{
system.out.println(�called�);
}
}

Here some client r calling and using this class. For each and every client call it will creates one instance now just I want one thing that how can I get count for that instance for myclass.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Hi,
Welcome to JavaRanch!
Add a static int member to the class. In each constructor, increment this variable. The variable will then represent the total number of objects created. You may also want a static "getCount()" method to fetch that info.


[Jess in Action][AskingGoodQuestions]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

This isn't a Servlets question, so I'm requesting a chang of venue.
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vinod Chandana
Ranch Hand

Joined: Aug 26, 2003
Posts: 59
Make the static int variable volatile, so that u dont have problems when two instances are being created at the same time.
- Vinod.
Originally posted by Ernest Friedman-Hill:
Hi,
Welcome to JavaRanch!
Add a static int member to the class. In each constructor, increment this variable. The variable will then represent the total number of objects created. You may also want a static "getCount()" method to fetch that info.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: count for instance of a class
 
Similar Threads
weird syntax
getInstance() Is that really a method()
count for instance of a class
Incompatible types
Java Design patterns