10 Replies Latest reply: Jun 2, 2008 10:40 PM by 807601 RSS

    Trying to count number of objects created

    807601
      I want to be able to count the total number of objects created on a class.
      public class Bear {
           static int i=0;
           Bear(){
                i = i + 1;
                }
      }
      public class BearDemo {
      
           public static void main(String[] args) {
                Bear x = new Bear();
                Bear y = new Bear();
                Bear z = new Bear();
                System.out.println("Number of bears is : " +Bear.i);
           }
      }
      When compiling, I get error message:
      java.lang.NoClassDefFoundError: BearDemo
      Exception in thread "main"

      I don't know where I went wrong :(
        • 1. Re: Trying to count number of objects created
          807601
          classpath problem while running.
          if BearDemo.class exists try running "java -cp . BearDemo" in the folder containing the file (assuming its not in a package).
          • 2. Re: Trying to count number of objects created
            807601
            I tried compiling using the jdk and it worked.
            However, I can't understand why I am having the problem in Eclipse.
            As far as I know, from the package explorer, they are both in the same working folder using the default package.
            • 3. Re: Trying to count number of objects created
              807601
              The problem is that the variable i exists within the class Bear, that is, it's contained within an object of type Bear. So when a Bear object is constructed, it has a variable i with a value of 1. Each Bear object has its own field i. In your code, you construct objects x, y, and z of type Bear. Each instance of class Bear, ( each object of type Bear ), has a variable i. Since you haven't created an object named Bear, the variable Bear.i means nothing. Variable names come from object names, not class names, since classes contain no actual variables. So what you have here are variables x.i, y.i, and z.i, each with a value of one.

              The way you want to think about it is this: Objects are data defined by a framework found in a class. Classes hold no data, they simply define the nature of an object.

              Although none of this really relates to your classpath error.

              Edited by: drshotgun on Jun 2, 2008 1:09 AM
              • 4. Re: Trying to count number of objects created
                807601
                DrShotgun,
                Did you notice that the variable i is static???
                • 5. Re: Trying to count number of objects created
                  807601
                  I just re-created the source folder in Eclipse. The program now runs fine.
                  Not sure why I was getting the main exception error, but I'll move on.
                  The code is legitimate.
                  • 6. Re: Trying to count number of objects created
                    3004
                    izzy75 wrote:
                    Not sure why I was getting the main exception error, but I'll move on.
                    The code is legitimate.
                    The compiler doesn't lie. If it reports a problem, then that problem exists.
                    • 7. Re: Trying to count number of objects created
                      807601
                      jverd wrote:
                      izzy75 wrote:
                      Not sure why I was getting the main exception error, but I'll move on.
                      The code is legitimate.
                      The compiler doesn't lie. If it reports a problem, then that problem exists.
                      I never said the compiler lied. I said, the code is legitimate.
                      You can even compile it on your machine, and see if it works.
                      There was something wrong with the configuration of the Eclipse IDE which I could not figure it out.
                      So I just started from scratch.
                      • 8. Re: Trying to count number of objects created
                        3004
                        izzy75 wrote:
                        jverd wrote:
                        izzy75 wrote:
                        Not sure why I was getting the main exception error, but I'll move on.
                        The code is legitimate.
                        The compiler doesn't lie. If it reports a problem, then that problem exists.
                        I never said the compiler lied. I said, the code is legitimate.
                        If you state that you're getting an error and also that the code is legit, then the implication is that you think the compiler is wrong. It's not. Either your code is not "legitmiate" or else you're not compiling the code you think you are.
                        You can even compile it on your machine, and see if it works.
                        Not that interested.
                        There was something wrong with the configuration of the Eclipse IDE which I could not figure it out.
                        So I just started from scratch.
                        Ah, I see. So your point was that the code was good but that you made an error in configuring your IDE. Fair enough.
                        • 9. Re: Trying to count number of objects created
                          notivago
                          I would like to point here to the newcomer reader that what drshotgun said is NOT true and classes can hold data in static fields, just like the original poster did(and john has pointed out), although it is a better style to make it explicit that you are using the static field by using full static syntax:
                          public class Bear {
                               private static int counter = 0;
                               
                               public static int getCount() {
                                    return counter;
                               }
                               
                               public Bear() {
                                    Bear.counter++;
                               }
                               
                               public static void main(String[] args) {
                                    new Bear();
                                    new Bear();
                                    new Bear();
                                    System.out.println( Bear.getCount() );
                               }
                          }
                          • 10. Re: Trying to count number of objects created
                            807601
                            notivago wrote:
                            I would like to point here to the newcomer reader that what drshotgun said is NOT true and classes can hold data in static fields, just like the original poster did(and john has pointed out), although it is a better style to make it explicit that you are using the static field by using full static syntax:
                            public class Bear {
                                 private static int counter = 0;
                                 
                                 public static int getCount() {
                                      return counter;
                                 }
                                 
                                 public Bear() {
                                      Bear.counter++;
                                 }
                                 
                                 public static void main(String[] args) {
                                      new Bear();
                                      new Bear();
                                      new Bear();
                                      System.out.println( Bear.getCount() );
                                 }
                            }
                            Nicely Done!

                            thanx, Izzy.