By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,804 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,804 IT Pros & Developers. It's quick & easy.

Problem using array as instance variable

P: 94
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):

Expand|Select|Wrap|Line Numbers
  1. public class HugeInteger
  2. {
  3.     /* Digits that form the number: */
  4.     private int digits[];
  5.  
  6.     public HugeInteger( String numberAsString )
  7.     {
  8.         this();
  9.  
  10.         int beginningOf_i = digits.length - 1;
  11.         int endOf_i = beginningOf_i - numberAsString.length() + 1;
  12.         int i;
  13.         int j;
  14.         int beginningOf_j = numberAsString.length() - 1;
  15.  
  16.         for ( i = beginningOf_i, j = beginningOf_j ; i >= endOf_i ;
  17.             i--, j-- )
  18.         {
  19.             digits[ i ] =
  20.                 Character.getNumericValue( numberAsString.charAt( j ) );
  21.         }
  22.     }
  23.  
  24.     /* Initializes all digits to 0: */
  25.     public HugeInteger()
  26.     {
  27.         digits = new int[ 40 ];
  28.  
  29.         for ( int i = 0 ; i < digits.length ; i++ )
  30.         {
  31.             digits[ i ] = 0;
  32.         }
  33.     }
  34. } /* 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!
Aug 16 '13 #1
Share this Question
Share on Google+
4 Replies


P: 94
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.
Aug 17 '13 #2

Nepomuk
Expert 2.5K+
P: 3,112
In line 10 you try to access digits.length without having initialized it. Could that be the problem?
Aug 21 '13 #3

P: 94
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?
Aug 21 '13 #4

Nepomuk
Expert 2.5K+
P: 3,112
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?
Aug 21 '13 #5

Post your reply

Sign in to post your reply or Sign up for a free account.