count the number of objects created by java

会有一股神秘感。 提交于 2021-02-05 07:57:06

问题


I'm trying to count the number of objects created but it always returns 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

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:


You need to make numb static so that there will only be one copy for every instance of the class. As it is, every single Employee object has its own copy of numb.

Also instead of creating a method to up the counter why not just put it in the constructor:

public Employee() {
   numb++;
}



回答2:


numb is an instance variable, meaning that each Employee object will have its own numb, that will be initialized by 0.

If you want all the Employee instances to share the same numb, you should make it static.




回答3:


// 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); 
    } 
} 



回答4:


Since static members initialized only once and it will be same for each and every instances of class.

class YourClass {

    private static int numb;

    public YourClass() {
        //...
        numb++;
    }

    public static int counter() {
        return numb;
    }
}

So simple;-




回答5:


make this modifications

  1. make numb static like, public int numb=0;,
  2. remove numb++; from method count() and
  3. create constructor public Employee{numb++;}


来源:https://stackoverflow.com/questions/52301869/count-the-number-of-objects-created-by-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!