Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
The moose likes Servlets 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 The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "count for instance of a class " Watch "count for instance of a class " New topic

count for instance of a class

Raja Sekhar

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()

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

Joined: Jul 08, 2003
Posts: 24189

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

Joined: Jan 10, 2002
Posts: 62315

This isn't a Servlets question, so I'm requesting a chang of venue to the Java In General (beginner) forum. Please continue the conversation here.
[ October 20, 2003: Message edited by: Jessica Sant ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: count for instance of a class