11 Replies Latest reply: Sep 5, 2007 10:26 AM by abillconsl RSS

    Problem with Static Initializer

    807605
      I am receiving a java.lang.NoClassDefFoundError exception from the following class when I try to use it
      class SomeClass
      {
         private BigDecimal num;
         static final BigDecimal ONE_HUNDRED;
      
         static
         {
            ONE_HUNDRED =  new BigDecimal (100);
         }
      
         SomeClass (BigDecimal number)
         {
            this.num  = number.divide (ONE_HUNDRED);
         }
      }
      Any idea why I am getting this exception?
        • 1. Re: Problem with Static Initializer
          807605
          Are you sure it's BigDecimal and not, say, SomeClass, that's not found.
          Why use the static init, by the way, instead of simply writing:
          static final BigDecimal ONE_HUNDRED = new BigDecimal(100);
          • 2. Re: Problem with Static Initializer
            807605
            I am definetly getting the exception on "SomeClass.

            The problem goes away however if I use use "new"
            SomeClass (BigDecimal number)
             {
                  this.num  = number.divide (new BigDecimal (100));
             }
            I did use:
            static final BigDecimal ONE_HUNDRED = new BigDecimal(100);
            at first but switched to the "static" to see if it would avoid the exception.
            • 3. Re: Problem with Static Initializer
              807605
              Curious.

              If you are serious about getting help, post a small (<1 page)
              example program that forum members can copy and
              compile.
              • 4. Re: Problem with Static Initializer
                Darryl Burke
                I ran this code and didn't get any Exception

                CallSomeClass.java
                import java.math.BigDecimal;
                
                public class CallSomeClass
                {
                    
                    public static void main(String[] args)
                    {
                        SomeClass callNum = new SomeClass(new BigDecimal(1000));
                        System.out.println(callNum.getNum().toString());
                    }
                }
                SomeClass.java
                import java.math.BigDecimal;
                class SomeClass
                {
                    private BigDecimal num;
                    static final BigDecimal ONE_HUNDRED = new BigDecimal(100);
                 
                    SomeClass (BigDecimal number)
                    {
                        this.num  = number.divide (ONE_HUNDRED);
                    }
                   
                
                    BigDecimal getNum()
                    {
                        return num;
                    }
                }
                Regards, Darryl
                • 5. Re: Problem with Static Initializer
                  807605
                  Hi Darryl,

                  Thanks. I just tried it standalone and did not get any exception. It must be my environement. I am getting the exception during the running of my Junit unit tests under maven. It's just strange that under the same environment using the "new" causes the problem to go away.
                  • 6. Re: Problem with Static Initializer
                    Darryl Burke
                    under the same environment using the
                    "new" causes the problem to go away.
                    whipsaw, that really is strange. I tried this out because I couldn't see anything wrong with your codes (because there wasn't).

                    I'm wondering whether the problem might possibly go away if you use fully qualified paths instead of import statements in SomeClass. (for BigDecimal)

                    Just about the only thing that comes to mind to provide a degree of independence from the environment.
                    • 7. Re: Problem with Static Initializer
                      807605
                      I'm wondering whether the problem might possibly go away if you use fully qualified paths instead of import statements in SomeClass. (for BigDecimal)
                      Occasionally people define a class with the same name as a library class and then get into trouble -- for example, they define a class named BigDecimal.

                      To avoid this:
                      1. Don't name a class BigDecimal!
                      2. Use fully qualified class names:
                      private java.math.BigDecimal value;
                      3. Or import the class specifically, instead of import java.math.*;
                      import java.math.BigDecimal;
                      • 8. Re: Problem with Static Initializer
                        Darryl Burke
                        Hello BDLH

                        I was also wondering whether the compiler was attempting to use a "BigDecimal" from another package or the development environment.

                        But I still can't understand why initializing the variable inside the main method would be any different from initializing it in a static final declaration, and how that would lead to a ClassNotFoundException for SomeClass.

                        Could you possibly throw some light (only for my learning experience).

                        Thanks, Darryl

                        edit Reviewed #2, he didn't actually initialize a variable in main(...).

                        Message was edited by:
                        Darryl.Burke
                        • 9. Re: Problem with Static Initializer
                          807605
                          You shouldn't wonder about problems listed in the forum when they're this sketchy.
                          The problem turns out to be something else.

                          In this case, I would have wanted to see what happens when fully qualified names are used.
                          • 10. Re: Problem with Static Initializer
                            abillconsl
                            So, in line with other comments, have you tried the following:
                            class SomeClass
                            {
                               private BigDecimal num;
                               static final BigDecimal ONE_HUNDRED =  new java.math.BigDecimal (100);
                             
                               SomeClass (BigDecimal number)
                               {
                                  this.num  = number.divide (ONE_HUNDRED);
                               }
                            }
                            ?