2 Replies Latest reply: Sep 25, 2013 9:07 AM by Tolls RSS

    Enum Object Memory Usage?

    user8729783

      Hi All

       

      I am Using an Enum class to use constants.

       

      I created one Enum class with 20 constants for Ex

      public enum TestEnum {

       

         One {

              public String toString() {

                  return "One";

              }

          },

          TWO {

              public String toString() {

                  return "TWO";

              }

          },

        

      }

       

      When It is compiled compiler creating one class for whole class and again it is creating one class for each constant.

       

      Is it takes more memory than a final simple java class.

       

      How Can I get How much memory it is Consuming.

       

      which is the best  simple Java class or enum class to maintain constants.

       

      Please Suggest me ASAP.

       

      Thanks & Regards

      user8729783

        • 1. Re: Enum Object Memory Usage?
          EJP

          > When It is compiled compiler creating one class for whole class

           

          Correct.

           

          > and again it is creating one class for each constant.

           

          Wrong. It is creating one object for each constant.

          • 2. Re: Enum Object Memory Usage?
            Tolls

            >> and again it is creating one class for each constant.

             

            >Wrong. It is creating one object for each constant.

             

            They've got each enum overriding toString.

            The compiler will have generated a TestEnum$n.class for each of them.

             

            Not sure why that should be a surprise for them, though.