2

I am quite confused on how my array is formed. This is what is written to make the array.

public class TestProgram {

public static final Room[] rooms = new Room[]
{
  new Room ("GARDEN0001", "NorthWest Garden View", 45.00),
  new Room ("GARDEN0002", "SouthEast Garden View", 65.0),
  new Room ("GARDEN0003", "North Garden View", 35),
  new Room ("GARDEN0004", "South Garden View", 52),
  new Room ("GARDEN0005", "West Garden View", 35),
  new Room ("GARDEN0006", "East Garden View", 35)
};

and I think the data comes from this class:

public class Room {
    public Room(String roomId, String description, double dailyRate){
        this.roomId = roomId;
        this.description = description;
        this.dailyRate = dailyRate;
        this.status = 'A';
    }

Us this a way of declaring an array from a constructor from another class or something? I am pretty confused but it works.

Can someone explain to me why is it written as Room[] rooms = new Room[]?

Bohemian
  • 365,064
  • 84
  • 522
  • 658
SadJavaBoi
  • 69
  • 1
  • 7

3 Answers3

2

Breaking it down:

Room[] rooms

Declares a static field (a class variable) with name rooms and type Room[] which is an array of Room objects.

= new Room[] {new Room...};

Initializes the array with Room objects and also defines the array's size (which in java can not be changed).

Bohemian
  • 365,064
  • 84
  • 522
  • 658
1

This is inline initialization of an array.

"Normally" you would declare an array of certain length and then initialize it or put inside the relevant data (maybe in a loop). However, it can be done in one line, like here - an easier example would be:

int[] numbers = new int[]{2, 3, 5};

Here we have the same case, just the "numbers" are Room objects (each created and initialized here) and not int numbers.

Additionally, pay attention that the array is final - once you assign something to it, that's it (you can change the data in the array, but not its length - or change it to point do a different array). If your data is final - it's clearer to assign it all at once.

Lastly, it's static - you cannot do it in a constructor, unless you make sure it's NULL (the first time it will work, but every other after that will fail, since you'd be trying to reassign a common final object).

All in all, inline declaration and initialization of a final static array. :-)

Iamsomeone
  • 213
  • 2
  • 15
st2rseeker
  • 474
  • 1
  • 7
  • 24
0

Here you declare and initialize an array of Room. Look -

Room rooms[] // declaration of 'Room' array    

After that -

new Room[]
{
  new Room ("GARDEN0001", "NorthWest Garden View", 45.00),
  new Room ("GARDEN0002", "SouthEast Garden View", 65.0),
  new Room ("GARDEN0003", "North Garden View", 35),
  new Room ("GARDEN0004", "South Garden View", 52),
  new Room ("GARDEN0005", "West Garden View", 35),
  new Room ("GARDEN0006", "East Garden View", 35)
}; //initialization of the array.  

Now here you Room is created with the array element you mentioned here and get the size of 6. You array declaration and initialization is actually equivalent to the following code snippet -

int[] intArray = new int[]{11,222,333, 444};    

And yes your data into the array rooms come through Room class constructor. And look the Room object created by this -new Room ("GARDEN0001", "NorthWest Garden View", 45.00) will be found in at room[0]. Similarly -

room[1] <-- new Room ("GARDEN0002", "SouthEast Garden View", 65.0)
room[2] <-- new Room ("GARDEN0003", "North Garden View", 35)
room[3] <-- new Room ("GARDEN0004", "South Garden View", 52)
room[4] <-- new Room ("GARDEN0005", "West Garden View", 35)
room[5] <-- new Room ("GARDEN0006", "East Garden View", 35)
Razib
  • 10,057
  • 10
  • 46
  • 71