Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7602391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:24:01+00:00 2026-05-30T23:24:01+00:00

How can I make the object array named rooms accessible to the static method

  • 0

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

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T23:24:02+00:00Added an answer on May 30, 2026 at 11:24 pm

    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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Since there is no way that you can make the flash object transparent, there
How can I make a variable (object) available for the whole lifetime of the
How can i make a cached object re-cache it self with updated info when
Can I make C# start Outlook in the code? In VB6 we use object
How can I make a ForeignKey refer back to the object itself? I'm trying
Given an instantiated JavaScript object/class can I make an Ajax call from that class
I want to make a generic method that will take either a single object
How can I create a property from a given argument inside a object's method?
I can make a DAO recordset in VB6/Access do anything - add data, clean
I can make Firefox not display the ugly dotted focus outlines on links with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.