[英]count the number of objects created by java
I'm trying to count the number of objects created but it always returns 1.我正在尝试计算创建的对象数,但它总是返回 1。
public class Drivertwo {
public static void main(String[] args) {
Employee newEmp = new Employee();
Employee newEmp2 = new Employee();
Calculate newcal = new Calculate();
Clerk newclerk = new Clerk();
float x;
int y;
newEmp.setEmp_no(2300);
newEmp.setEmp_name("W.Shane");
newEmp.setSalary(30000);
newEmp.counter();
newEmp2.setEmp_no(1300);
newEmp2.setEmp_name("W.Shane");
newEmp2.setSalary(50000);
newEmp2.counter();
newclerk.setEmp_name("Crishane");
newclerk.setEmp_no(1301);
newclerk.setGrade(2);
newclerk.setSalary(45000);
newclerk.counter();
System.out.println("Salary is:" + newcal.cal_salary(newclerk.getSalary(), newclerk.getEmp_no()));
System.out.println("Name is:" + newclerk.getEmp_name());
System.out.println("Employee number is:" + newclerk.getEmp_no());
System.out.println("Employee Grade is:" + newclerk.getGrade());
System.out.println("No of objects:" + newEmp.numb);
This is my class with the main method这是我的课程,主要方法
public class Employee {
private int salary;
private int emp_no;
private String emp_name;
public int numb=0;
public int getSalary() {
return salary;
}
public int getEmp_no() {
return emp_no;
}
public String getEmp_name() {
return emp_name;
}
public void setSalary(int newSalary) {
salary = newSalary;
}
public void setEmp_no(int newEmp_no) {
emp_no = newEmp_no;
}
public void setEmp_name(String newEmp_name) {
emp_name = newEmp_name;
}
}
public int counter() {
numb++;
return numb;
This is my Employee class这是我的 Employee 类
I tried to run counter in my employee class as a starter but it always returns 1. I know I can make a counter in main class and everytime I make a new object I can get the counter but I want to automatically increase the numb by 1 when an object is made.我试图在我的员工类中运行计数器作为启动器,但它总是返回 1。我知道我可以在主类中创建一个计数器,每次我创建一个新对象时,我都可以获得计数器,但我想自动将 numb 增加 1制作对象时。
You need to make numb
static so that there will only be one copy for every instance of the class.您需要将
numb
设为静态,以便该类的每个实例只有一个副本。 As it is, every single Employee
object has its own copy of numb
.事实上,每个
Employee
对象都有自己的numb
副本。
Also instead of creating a method to up the counter why not just put it in the constructor:另外,与其创建一个增加计数器的方法,不如将它放在构造函数中:
public Employee() {
numb++;
}
numb
is an instance variable, meaning that each Employee
object will have its own numb
, that will be initialized by 0
. numb
是一个实例变量,这意味着每个Employee
对象都有自己的numb
,它将被0
初始化。
If you want all the Employee
instances to share the same numb
, you should make it static
.如果您希望所有
Employee
实例共享相同的numb
,您应该将其设为static
。
// Java program Find Out the Number of Objects Created
// of a Class
class Test {
static int noOfObjects = 0;
// Instead of performing increment in the constructor instance block is preferred
//make this program generic. Because if you add the increment in the constructor
//it won't work for parameterized constructors
{
noOfObjects += 1;
}
// various types of constructors
public Test()
{
}
public Test(int n)
{
}
public Test(String s)
{
}
public static void main(String args[])
{
Test t1 = new Test();
Test t2 = new Test(5);
Test t3 = new Test("Rahul");
System.out.println(Test.noOfObjects);
}
}
I tried to run counter in my employee class as a starter but it always returns 1
我尝试在我的员工类中作为启动器运行计数器,但它总是返回1
It returns 1 because numb
is an instance variable, meaning that each instance/object that you make of Employee
class has its own copy of numb
variable initially equal to zero
. 它返回1,因为
numb
是一个实例变量,这意味着您为Employee
类创建的每个实例/对象都有自己的numb
变量副本,最初zero
。 When you call counter
method on the newly created instance of Employee
class, numb
variable gets incremented to 1
and that gets returned. 当您在新创建的
Employee
类实例上调用counter
方法时, numb
变量会增加到1
并返回。
Same process happens every time you create an instance of Employee
class. 每次创建
Employee
类的实例时都会发生相同的过程。
i want to automatically increase the numb by 1 when an object is made
我想在制作一个物体时自动将麻木增加1
Making numb
static variable will solve this problem because then numb
will be a class variable and class variables are shared between each instance of that class. 制作
numb
静态变量将解决这个问题,因为numb
将是一个类变量,类变量在该类的每个实例之间共享。
Check out the Difference between instance and class variables 查看实例和类变量之间的差异
make this modifications做这个修改
numb
static like, public int numb=0;
numb
静态像, public int numb=0;
, numb++;
numb++;
from method count()
andcount()
和public Employee{numb++;}
public Employee{numb++;}
Since static
members initialized only once and it will be same for each and every instances of class.由于
static
成员仅初始化一次,并且对于类的每个实例都是相同的。
class YourClass {
private static int numb;
public YourClass() {
//...
numb++;
}
public static int counter() {
return numb;
}
}
So simple;-很简单;-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.