7 Replies - 31648 Views - Last Post: 05 February 2010 - 07:08 PM Rate Topic: -----

#1 vyom.dev  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 74
  • Joined: 13-January 10

count number of objects in a class

Posted 05 February 2010 - 01:03 AM

hello i want to know is there any method or rule to count the number of objects used in a class in java???

thanx
Is This A Good Question/Topic? 0
  • +

Replies To: count number of objects in a class

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2734
  • View blog
  • Posts: 11,515
  • Joined: 20-September 08

Re: count number of objects in a class

Posted 05 February 2010 - 01:34 AM

How are you doing it 'manually' at the moment?
Was This Post Helpful? 0
  • +
  • -

#3 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: count number of objects in a class

Posted 05 February 2010 - 02:01 AM

how are you counting the objects at present now?
what logic you have used??
please let us know
Was This Post Helpful? 0
  • +
  • -

#4 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: count number of objects in a class

Posted 05 February 2010 - 02:18 AM

View Postvyom.dev, on 05 February 2010 - 12:03 AM, said:

hello i want to know is there any method or rule to count the number of objects used in a class in java???

thanx


To count how many objects have been created IN a class:
Create a variable initialized at 0, and then every time an object is created, add 1 to that variable?

Or, to count how many objects have been created FROM a class, create a static variable initialized at 0, and in each and every constructor for that class, increment it by 1. Like so:
public Cars {

static int carInstances = 0;

public Cars() {
carInstances += 1;
}

public static void main(String[] args) {}
}


Now, in the above code, you can use the variable Cars.carInstances to see how many Cars have been created. Neato.
Was This Post Helpful? 0
  • +
  • -

#5 vyom.dev  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 74
  • Joined: 13-January 10

Re: count number of objects in a class

Posted 05 February 2010 - 04:04 AM

okie i got it the constructor is used to create object and static is used because static method can only call the static variable...but still i have some doubts about the bold part and how it increments...plz explain
class object
{
static int ob = 0;
public object() 
{
ob = ob + 1;
}
public static void main(String[] args) 
{
object ob1 = new object();
object ob2 = new object();
System.out.println([b]object.ob[/b]);
}
}




thanx

This post has been edited by vyom.dev: 05 February 2010 - 04:12 AM

Was This Post Helpful? 0
  • +
  • -

#6 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: count number of objects in a class

Posted 05 February 2010 - 04:30 AM

View Postvyom.dev, on 05 February 2010 - 10:04 AM, said:

but still i have some doubts about the bold part and how it increments...



The increments happens in the constructor:

public object() {
  ob = ob + 1;
}



When doing "object.ob", you are actually getting the value of the static variable "ob".

On another note, avoid calling your class "object": "Object" is the base for all Java classes. Also, the convention for writing class names is to uppercase the first letter ("MyClass"); you don't have to follow it, but it's preferrable especially when exchanging with peers.
Was This Post Helpful? 1
  • +
  • -

#7 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: count number of objects in a class

Posted 05 February 2010 - 06:14 PM

View Postvyom.dev, on 05 February 2010 - 03:04 AM, said:

okie i got it the constructor is used to create object and static is used because static method can only call the static variable


Actually, you use static in this instance because a static variable is a class variable, separate from the instances of the class. If you create a class with two variables, one static, one not, EVERY instance of that class (objects created from that class) gets its own version of that variable. But with the static variable, it belongs to the class, and there is ONLY ONE. Although any object FROM that class can reference it. EX:
public class Whatever {

static int varA = 0;
int varB = 0;

public static void main(String[] argumments) {
Whatever one = new Whatever();
Whatever two = new Whatever();
}
}


In the preceding example, one and two have their own varB. one.varB can hold a different value from two.varB. But there is only one varA.
Whatever.varA == one.varA == two.varA

[quote name='vyom.dev' date='05 February 2010 - 03:04 AM' timestamp='1265367854' post='916214']
but still i have some doubts about the bold part and how it increments...plz explain[/qoute]
So, to explain this, a constructor (a method that has the same name as the class) is used to help build objects from that class. Every time you use the word new in Java, you are creating an object from a class. So in your example, when you declare ob1 as a new object, it looks to the constructor, which tells it add 1 to ob. Then, when you declare ob2 as another new object, it looks to the constructor. So that's where it increments in your program.

This post has been edited by xor-logic: 05 February 2010 - 06:21 PM

Was This Post Helpful? 2
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,032
  • Joined: 15-July 08

Re: count number of objects in a class

Posted 05 February 2010 - 07:08 PM

xor-logic is absolutely right. Now to blow my own horn. Read this article that I wrote about static methods and variables. It should help to clear up many of your questions. http://www.dreaminco...&showentry=2019

Enjoy!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1