How to count objects in a single class?

Discussion in 'Java Language Basics/Fundamentals' started by Nalan, Dec 3, 2009.

  1. Nalan

    Nalan New Member

    how to count How many object in a single class?
    I need simple program for count object in a class...
     
  2. vsreddy.k

    vsreddy.k New Member

    Re: how to count How many object in a single class?

    hi

    By using static variable we can find no. of Objects are created in a Single class

    plz see the example program:




    public class One
    {
    static int i=0;
    One()
    {
    i++;
    }
    public static void main(String[] ar)throws Exception
    {
    One o1=new One();
    One o2=new One();
    One o3=new One();
    One o4=new One();
    System.out.println("One more Collection ");
    One o[]={new One(),new One(),new One(),new One(),new One()};
    System.out.println("No of Objects are instantiated in a class");
    System.out.println(i);
    }
    }


    i think it is helpful for u


    :)bye
     
  3. anneroberts

    anneroberts Member

    At whatever point an object is produced of a class, its constructor will be conjured. At whatever point those constructor runs we increase the counter quality. Consequently amount for Questions made of a class equals of the counter quality.
     

Share This Page