Shehan Tearz
Shehan Tearz

Reputation: 143

count the number of objects created by java

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.

Upvotes: 1

Views: 10825

Answers (5)

Rahul Pandey
Rahul Pandey

Reputation: 11

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

Upvotes: 1

The Scientific Method
The Scientific Method

Reputation: 2436

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++;}

Upvotes: 0

Tanaji Kolekar
Tanaji Kolekar

Reputation: 127

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;-

Upvotes: 0

GBlodgett
GBlodgett

Reputation: 12819

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++;
}

Upvotes: 2

Mureinik
Mureinik

Reputation: 311063

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.

Upvotes: 1

Related Questions