Problem using array as instance variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stdq
    New Member
    • Apr 2013
    • 94

    Problem using array as instance variable

    Hi, I am writing a class called HugeInteger, and each object of this class is supposed to have its own array to represent the digits. However, I am having trouble compiling the program without declaring the array as static - but I shouldn't do that, since then the objects will share the array and that is not the goal. Here is the code for the class with its constructors - my problem is with the non-default constructor (the first one from top-down):

    Code:
    public class HugeInteger
    {
        /* Digits that form the number: */
        private int digits[];
        
        public HugeInteger( String numberAsString )
        {
            this();
            
            int beginningOf_i = digits.length - 1;
            int endOf_i = beginningOf_i - numberAsString.length() + 1;
            int i;
            int j;
            int beginningOf_j = numberAsString.length() - 1;
            
            for ( i = beginningOf_i, j = beginningOf_j ; i >= endOf_i ;
                i--, j-- )
            {
                digits[ i ] =
                    Character.getNumericValue( numberAsString.charAt( j ) );
            }
        }
        
        /* Initializes all digits to 0: */
        public HugeInteger()
        {
            digits = new int[ 40 ];
            
            for ( int i = 0 ; i < digits.length ; i++ )
            {
                digits[ i ] = 0;
            }
        }
    } /* End of public class HugeInteger. */
    The error message the compiler issues is "error: non-static variable digits cannot be referenced from a static context". How do I get around this issue without declaring the array as static? Thanks in advance!
  • stdq
    New Member
    • Apr 2013
    • 94

    #2
    I was able to get around the problem. Interestingly, my problem was only when accessing the length of the array field, so I created a static field with the length, which is always 40 and thus can be shared among all objects. Then, instead of referring to digits.length, I simply referred to that new field. The compiler didn't complain, then.

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      In line 10 you try to access digits.length without having initialized it. Could that be the problem?

      Comment

      • stdq
        New Member
        • Apr 2013
        • 94

        #4
        Hey, Nepomuk! I don't think so. In line 8, I call the default constructor (lines 26-33), so the length field of the array is then initialized because of the body of that constructor. Unless I'm not calling the default constructor properly (maybe the statement is incorrect). What do you think?

        Comment

        • Nepomuk
          Recognized Expert Specialist
          • Aug 2007
          • 3111

          #5
          You're right, my mistake. Indeed, I added a main function to your code and ran it successfully. Were you trying to access the digits variable from outside at some point? Where exactly was the error thrown?

          Comment

          Working...