简体   繁体   中英

Java accessing instance variables

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];
    }

}

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 4 th 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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM