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):
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!
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. */
Comment