I wrote a small class that creates a report object containing 3 arrays. At creation of the object these arrays are initialised with values. However when i test the class to see for example what's in the departments array, it prints out that the array elements are null. why?
class Report
{
// declare instance variables (arrays)
public String[] departments = new String[4] ;
public double[] grossTotals = new double[4] ;
public double[] taxTotals = new double[4] ;
// constructor
public Report(){
// declare, create and initialise all in one statement
String[] departments = {"Accounting", "Sales", "HR", +
"Administration"} ;
double[] grossTotals = {0.0, 0.0, 0.0, 0.0} ;
double[] taxTotals = {0.0, 0.0, 0.0, 0.0} ;
} // END constructor
} // class Report
Test Class:
class TestReport
{
public static void main(String[] args) {
// create report object
Report r = new Report();
for (int i = 0; i <= 3 ; i++ )
{
System.out.println(r.departments[i]) ;
}
} //end main
} // end test class
thanks
Baba
public finaldoesn't fix that, as the contents can still be changed. With arrays it's best to make them private (and possiblystatic finalas well) where possible.