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
user969729
2993 gold badges7 silver badges14 bronze badges
1 Answer 1
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:
- Static members can be accessed as long as that
Classexists. However, in order to access instance members (variables which belong to an instance of that class) you need an instance of the class. - Use
Rooms[] rooms = new Rooms[4];. Notice that array brackets are moved toTypepart, 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. - You are defining
roomsarray with the size of 3 however you are adding 4 elements to it. This will causeArrayIndexOutOfBoundsexception. - You should make your members
privateto achieve Encapsulation. You can providepublicorprotectedmethods to retrieve thoseprivatemembers from out of your class. - When an Array contains
nelements, maximum number of consecutive iterations that you can make on that array is alsonthat's for sure. That means in aforloop make sure that your loop condition isn-1for the last iteration. Remember, there is no4th element in a size4array.
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
Cengiz Can
1,3021 gold badge15 silver badges31 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
Dave Newton
The variable needs to be static to be accessible by a static method.
Dave Newton
Member properties are not available to static methods without an instance; your edit does not address this--your code will not compile.
Dave Newton
Now it does, although there's still a mic of the old and new code/verbiage in the answer.
Cengiz Can
@DaveNewton Thanks for noticing. I tried to improve it after your comment.
lang-java