how to know how many objects a class has generated?
2011-12-02 07:28
302 查看
How to know how many objects has been generated for a class?
We need use the “static” knowledge. Every time you new an object, you will call the constructor(s) of the class. And the “static” property can be updated every time.
So, see below sample:
——————————
package ch.allenstudy.newway01;
public class TestStatic_1
{
public static void main(String[] args)
{
System.out.println(“Now the object number is: ” + AA .getCnt());
AA aa1 = new AA();
System.out.println(“Now the object number is: ” + AA .getCnt());
AA aa2 = new AA(3);
System.out.println(“Now the object number is: ” + AA .getCnt());
}
}
class AA
{
private int i;
private static int cnt = 0;
public AA()
{
++cnt;
}
public AA(int i)
{
this.i = 0;
++cnt;
}
public static int getCnt()
{
return cnt; //also can write as: return AA.cnt;
}
}
——–REsult———
Now the object number is: 0
Now the object number is: 1
Now the object number is: 2
———————–
要知道,只要你实例化一个对象,就要调用类的 Constructor。当然,一个实例化对象,只能调用一个constructor,在这个例子中,要么调用 AA(),要么调用 AA(i)。
所以我们在每个Constructor里面都写了 ++cnt。这样,只要你实例化,我们 cnt 的值就加 1。因为 cnt 是一个静态属性,所以我们可以直接 return AA.cnt。这里简写为 return cnt
We need use the “static” knowledge. Every time you new an object, you will call the constructor(s) of the class. And the “static” property can be updated every time.
So, see below sample:
——————————
package ch.allenstudy.newway01;
public class TestStatic_1
{
public static void main(String[] args)
{
System.out.println(“Now the object number is: ” + AA .getCnt());
AA aa1 = new AA();
System.out.println(“Now the object number is: ” + AA .getCnt());
AA aa2 = new AA(3);
System.out.println(“Now the object number is: ” + AA .getCnt());
}
}
class AA
{
private int i;
private static int cnt = 0;
public AA()
{
++cnt;
}
public AA(int i)
{
this.i = 0;
++cnt;
}
public static int getCnt()
{
return cnt; //also can write as: return AA.cnt;
}
}
——–REsult———
Now the object number is: 0
Now the object number is: 1
Now the object number is: 2
———————–
要知道,只要你实例化一个对象,就要调用类的 Constructor。当然,一个实例化对象,只能调用一个constructor,在这个例子中,要么调用 AA(),要么调用 AA(i)。
所以我们在每个Constructor里面都写了 ++cnt。这样,只要你实例化,我们 cnt 的值就加 1。因为 cnt 是一个静态属性,所以我们可以直接 return AA.cnt。这里简写为 return cnt
相关文章推荐
- how to know whether an element has an class
- How to troubleshoot 'Procedure or function has too many arguments specified' in asp.net 2.0
- ctypes.ArgumentError: argument 1: <class 'TypeError'>: Don't know how to convert parameter 1
- ORMLite does not know how to store class java.util.ArrayList错误的解决
- How can selenium web driver get to know when the new window has opened and then resume its execution
- [Ubuntu] How to the virtualhost to a path which has many project folder?
- (Django) How do I set limit_choices_to another class from where the ManyToManyField points to? - django, django-models - TechQues.com
- [ASP.NET2.0][数据库更新]How to troubleshoot 'Procedure or function has too many arguments specified' in asp.net 2.0
- 译文:如何使用SocketAsyncEventArgs类(How to use the SocketAsyncEventArgs class)
- sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class异常解决方法
- The Apache Tomcat 5.5 Servlet/JSP Container Class Loader HOW-TO
- don’t know how to make ‘TargetCompilePass’
- How to use Class class
- How to know firmware version
- how to know the package name of android apk?
- how to get Class of primitive datatypes array . Class.forName(int[]) throws exception
- How to do run-time (or explicit) linking of C++ plug-in components and objects
- 【Rails】inverse_of在has_many和belongs_to中的用法
- How to properly implement ARC compatible and `alloc init` safe Singleton class?
- In Javascript Class, how to call the prototype method.(three method)