1

How can I make the object array named rooms accessible to the static method at the end called retrieveRoom(); I tryed public static Rooms rooms[] = new Rooms[3]. But I am just getting errors from that. Any help is appreciated.

public class MasterControlPanel{
 public static void main(String[] args){
 Rooms rooms[] = new Rooms[3];
 rooms[0] = new Rooms("Room U", 1, 4, 4);
 rooms[1] = new Rooms("Room U", 2, 4, 4);
 rooms[2] = new Rooms("Connector X", 3, 2, 4);
 rooms[3] = new Rooms("Connector U", 4, 2, 4);
 for(int x = 0; x <= rooms.length; x++){
 rooms[x].createLights();
 rooms[x].createWalls();
 }
 }
 public static Object retrieveRoom(int connectedRoom){
 connectedRoom -= 1;
 return rooms[connectedRoom];
 }

}

Cengiz Can
1,3021 gold badge15 silver badges31 bronze badges
asked Mar 4, 2012 at 21:41

1 Answer 1

3

Move

public static Rooms[] rooms = new Rooms[4];

out of your main() method, to the declarations section like this

public class MasterControlPanel {
 public static Rooms[] rooms = new Rooms[4];
 public static void main(String[] args) {
 ...
 }
}

This will define a rooms static member in your MasterControlPanel class.

Here are some other tips:

  1. Static members can be accessed as long as that Class exists. However, in order to access instance members (variables which belong to an instance of that class) you need an instance of the class.
  2. Use Rooms[] rooms = new Rooms[4];. Notice that array brackets are moved to Type part, this will prevent confusion in future. "Let's define An Array of Rooms with the name rooms". This is not required however a good thing to do.
  3. You are defining rooms array with the size of 3 however you are adding 4 elements to it. This will cause ArrayIndexOutOfBounds exception.
  4. You should make your members private to achieve Encapsulation. You can provide public or protected methods to retrieve those private members from out of your class.
  5. When an Array contains n elements, maximum number of consecutive iterations that you can make on that array is also n that's for sure. That means in a for loop make sure that your loop condition is n-1 for the last iteration. Remember, there is no 4th element in a size 4 array.

Here follows the code

public class MasterControlPanel {
 public static Room[] rooms = new Room[4];
 public static void main(String[] args) {
 rooms[0] = new Room("Room U", 1, 4, 4);
 rooms[1] = new Room("Room U", 2, 4, 4);
 rooms[2] = new Room("Connector X", 3, 2, 4);
 rooms[3] = new Room("Connector U", 4, 2, 4);
 for (int x = 0; x <= rooms.length-1; x++) {
 rooms[x].createLights();
 rooms[x].createWalls();
 }
 }
 public static Room retrieveRoom(int connectedRoom) {
 connectedRoom -= 1;
 return rooms[connectedRoom];
 }
}
class Room {
 // These are instance members for Room class
 private String roomString;
 private int roomInteger1, roomInteger2, roomInteger3;
 public Room(String string, int integer1, int integer2, int integer3) {
 // This is the constructor for Room object
 this.roomString = string;
 this.roomInteger1 = integer1;
 this.roomInteger2 = integer2;
 this.roomInteger3 = integer3;
 }
 public void createLights() {
 // This method creates lights
 }
 public void createWalls() {
 // This method creates walls
 }
 // These are Getters and Setters for Room members
 public String getRoomString() {
 return roomString;
 }
 public void setRoomString(String roomString) {
 this.roomString = roomString;
 }
 public int getRoomInteger1() {
 return roomInteger1;
 }
 public void setRoomInteger1(int roomInteger1) {
 this.roomInteger1 = roomInteger1;
 }
 public int getRoomInteger2() {
 return roomInteger2;
 }
 public void setRoomInteger2(int roomInteger2) {
 this.roomInteger2 = roomInteger2;
 }
 public int getRoomInteger3() {
 return roomInteger3;
 }
 public void setRoomInteger3(int roomInteger3) {
 this.roomInteger3 = roomInteger3;
 }
}
answered Mar 4, 2012 at 21:43
Sign up to request clarification or add additional context in comments.

4 Comments

The variable needs to be static to be accessible by a static method.
Member properties are not available to static methods without an instance; your edit does not address this--your code will not compile.
Now it does, although there's still a mic of the old and new code/verbiage in the answer.
@DaveNewton Thanks for noticing. I tried to improve it after your comment.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.