objects classes and arrays - why is it returning &

2019-03-05 17:19发布

问题:

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

回答1:

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
 } 


回答2:

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.