Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Method to count objects

  1. #1
    Member
    Join Date
    Jun 2010
    Posts
    75
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Method to count objects

    Is it possible to write a method that will count the number of object created?

    In the past, I have counted objects by declaring a integer count variable and incrementing each time that constructor is called. However, I now want to write a method that will count objects on its own. For instance:

    public class MyClass
    {
         private int numMyClass;
     
         public int countMyClassObjs
         {
              //Code that counts instances of MyClass
              return numMyClass;
         }
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Method to count objects

    In pure Java, no. Java has no way to peer into the memory without proper references. There is also a problem with counting the number of objects there are in this manner:

    When an object gets destructed, there is no mechanism implemented to decrease the count.

    The easy solution is to override the finalize method and decrease that counter whenever the object actually gets destructed (note: even though an object goes out of scope or has no more references to it, it does not get destructed right away)

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Method to count objects

    If this is just for profiling purposes, try out JVisualVM, comes with the JDK nowadays

    // Json

  4. #4
    Member
    Join Date
    Jun 2010
    Posts
    75
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Method to count objects

    Quote Originally Posted by Json View Post
    If this is just for profiling purposes, try out JVisualVM, comes with the JDK nowadays

    // Json
    I'm not sure I understand what you mean. Could you elaborate?

  5. #5
    Member
    Join Date
    May 2010
    Posts
    39
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default Re: Method to count objects

    Well for one, make the method static otherwise the feild "numMyClass" is a new value every instance, and in terms of removing, you need to override the "finalize" method, called by the garbage collector, and use that to decrease the value.

  6. #6
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Method to count objects

    Quote Originally Posted by helloworld922 View Post
    The easy solution is to override the finalize method and decrease that counter whenever the object actually gets destructed (note: even though an object goes out of scope or has no more references to it, it does not get destructed right away)
    But there is no guarantee that an object finalizer will ever be called, so it's not really a reliable solution.

Similar Threads

  1. Sentence and Letter Count Program
    By velop in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 10th, 2010, 12:10 AM
  2. select count(*)
    By jacinto in forum JDBC & Databases
    Replies: 4
    Last Post: March 2nd, 2010, 10:30 PM
  3. count how frequently each character occurs
    By sam in forum Java Theory & Questions
    Replies: 1
    Last Post: February 19th, 2010, 02:16 PM
  4. How can i add a new count to this source code ?
    By mm2236 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 30th, 2010, 10:21 PM
  5. count components inside a JPanel
    By dewboy3d in forum AWT / Java Swing
    Replies: 1
    Last Post: August 2nd, 2009, 03:17 PM