0

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

2
  • 1
    By the way, publicizing arrays (as in public String[]) is a bad habit. Other code can now change those arrays. Even public final doesn't fix that, as the contents can still be changed. With arrays it's best to make them private (and possibly static final as well) where possible. Commented Nov 14, 2010 at 16:40
  • HI, thanks that is correct. However then i would need a getter method to retrieve the data from the test class which my example does not have. Commented Nov 14, 2010 at 17:09

2 Answers 2

4

Make it like this

public Report(){
            // declare, create and initialise all in one statement
            this.departments = {"Accounting", "Sales", "HR", +
                                              "Administration"} ;
            this.grossTotals = {0.0, 0.0, 0.0, 0.0} ;
            this.taxTotals = {0.0, 0.0, 0.0, 0.0} ;

    } // END constru  

Actually you are creating new arrays objects local to your constructor those are getting initialized in constructor.

your class fields will be initialized using the code above .

If you have not done it by mistake than please refer to this doc also it will better clear your understandings

**

Update

:** Above code will give you illegal start of expression

Here is working code

 class  Report
 {
        // declare instance variables (arrays)
        public String[] departments = null;
        public double[] grossTotals = null;
        public double[] taxTotals = null;


        // constructor
        public Report(){
              this.departments = new String[]{"Accounting", "Sales", "HR", "Administration"} ;
         this.grossTotals = new double[]{0.0, 0.0, 0.0, 0.0} ;
         this.taxTotals = new double[]{0.0, 0.0, 0.0, 0.0} ;
    } // END constructor
 } 
Sign up to request clarification or add additional context in comments.

3 Comments

Perhaps nice to add that the problem he's having is called 'field shadowing'. That is, you create local variables with the same name as member/instance variables.
thanks very much for your help! i tested and it seems that your solution can be further simplified by removing "=null". seems it's not needed. thanks again!
@Baba thats true null is optional there
1

As the other answers have noted, your constructor created new local variables "shadowing" the instance variables instead of populating the instance variables with data.

However, the population code is a little different if you separate the declaration from the populating, and they didn't get that quite right. You also had a '+' character that didn't belong.

This code compiles and works (tested), and does basically what your looking for.

class  Report
{
       // declare instance variables (arrays)
       public String[] departments;
       public double[] grossTotals;
       public double[] taxTotals;


       // constructor
       public Report(){
           // populate instance variables
           departments = new String[]{"Accounting", "Sales", "HR",
                                             "Administration"} ;
           grossTotals = new double[]{0.0, 0.0, 0.0, 0.0} ;
           taxTotals = new double[]{0.0, 0.0, 0.0, 0.0} ;

   } // END constructor
} // class  Report

You can alternatively create the arrays in the declaration and then populate the array entries in the constructor or elsewhere.

With that approach the code could be something like:

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(){
           // populate instance variable entries
           departments[0] = "Accounting";
           departments[1] = "Sales";
           departments[2] = "HR";
           departments[3] = "Administration";

           for (int i = 0; i < 4; i++) {
               grossTotals[i] = 0.0;
               taxTotals[i] = 0.0;

           }
   } // END constructor
} // class  Report

As a third alternative, you can do all the initialization in the field declaration as follows:

class  Report
{
       // declare and populate instance variables (arrays)
       public String[] departments = new String[]{"Accounting", "Sales", "HR",
                                             "Administration"} ;
       public double[] grossTotals = new double[]{0.0, 0.0, 0.0, 0.0} ;
       public double[] taxTotals = new double[]{0.0, 0.0, 0.0, 0.0} ;

} // class  Report

In this case, you don't need to define the constructor at all, as it does nothing. An empty one will be supplied by Java.

1 Comment

Thanks Don! I did understand what the others were pointing out but their solution didn't work. So, the solution is to populate the array elements individually. It is surprising though because syntactically i couldn't see anything wrong with a one-line statement popuating the array. Thanks again! Baba (re +: that was just a layout thing for this post but i see i didn't need it)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.