Referencing an Object Inside an Object












0














I am creating a game in Java and need to create a list of all of the rooms in it.



I have a 'Rooms' class that has the code for the room, i.e. room name, items in the room etc.



In the Room class I want to have a static ArrayList that has all of the room objects in the whole game in there. This is needed for a method I am working on.



I have created an ArrayList field:



private static ArrayList<Rooms> listOfRooms = new ArrayList<Rooms>();


In the initialisation of each instance of Rooms, I wish to add that instance to the listOfRooms ArrayList.



I assume you start with listOfRooms.add(), but what would you actually put in the parameter to add the current object to the list of Rooms objects?










share|improve this question
























  • You need to get the static list and then add to it.
    – Nicholas K
    Nov 20 at 16:40










  • You'd better not use any collection as a static field, collections are mutable i.e. collie class able to modify internal state. If you really need a collection (I suppose you don't), try a singleton pattern. Enum singleton (Singleton of Bloch) choose is better. BTW, I suppose you've just need an enum.
    – Victor Gubin
    Nov 20 at 16:45


















0














I am creating a game in Java and need to create a list of all of the rooms in it.



I have a 'Rooms' class that has the code for the room, i.e. room name, items in the room etc.



In the Room class I want to have a static ArrayList that has all of the room objects in the whole game in there. This is needed for a method I am working on.



I have created an ArrayList field:



private static ArrayList<Rooms> listOfRooms = new ArrayList<Rooms>();


In the initialisation of each instance of Rooms, I wish to add that instance to the listOfRooms ArrayList.



I assume you start with listOfRooms.add(), but what would you actually put in the parameter to add the current object to the list of Rooms objects?










share|improve this question
























  • You need to get the static list and then add to it.
    – Nicholas K
    Nov 20 at 16:40










  • You'd better not use any collection as a static field, collections are mutable i.e. collie class able to modify internal state. If you really need a collection (I suppose you don't), try a singleton pattern. Enum singleton (Singleton of Bloch) choose is better. BTW, I suppose you've just need an enum.
    – Victor Gubin
    Nov 20 at 16:45
















0












0








0







I am creating a game in Java and need to create a list of all of the rooms in it.



I have a 'Rooms' class that has the code for the room, i.e. room name, items in the room etc.



In the Room class I want to have a static ArrayList that has all of the room objects in the whole game in there. This is needed for a method I am working on.



I have created an ArrayList field:



private static ArrayList<Rooms> listOfRooms = new ArrayList<Rooms>();


In the initialisation of each instance of Rooms, I wish to add that instance to the listOfRooms ArrayList.



I assume you start with listOfRooms.add(), but what would you actually put in the parameter to add the current object to the list of Rooms objects?










share|improve this question















I am creating a game in Java and need to create a list of all of the rooms in it.



I have a 'Rooms' class that has the code for the room, i.e. room name, items in the room etc.



In the Room class I want to have a static ArrayList that has all of the room objects in the whole game in there. This is needed for a method I am working on.



I have created an ArrayList field:



private static ArrayList<Rooms> listOfRooms = new ArrayList<Rooms>();


In the initialisation of each instance of Rooms, I wish to add that instance to the listOfRooms ArrayList.



I assume you start with listOfRooms.add(), but what would you actually put in the parameter to add the current object to the list of Rooms objects?







java class






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 16:47









Andrea

4,48411743




4,48411743










asked Nov 20 at 16:38









Link

43119




43119












  • You need to get the static list and then add to it.
    – Nicholas K
    Nov 20 at 16:40










  • You'd better not use any collection as a static field, collections are mutable i.e. collie class able to modify internal state. If you really need a collection (I suppose you don't), try a singleton pattern. Enum singleton (Singleton of Bloch) choose is better. BTW, I suppose you've just need an enum.
    – Victor Gubin
    Nov 20 at 16:45




















  • You need to get the static list and then add to it.
    – Nicholas K
    Nov 20 at 16:40










  • You'd better not use any collection as a static field, collections are mutable i.e. collie class able to modify internal state. If you really need a collection (I suppose you don't), try a singleton pattern. Enum singleton (Singleton of Bloch) choose is better. BTW, I suppose you've just need an enum.
    – Victor Gubin
    Nov 20 at 16:45


















You need to get the static list and then add to it.
– Nicholas K
Nov 20 at 16:40




You need to get the static list and then add to it.
– Nicholas K
Nov 20 at 16:40












You'd better not use any collection as a static field, collections are mutable i.e. collie class able to modify internal state. If you really need a collection (I suppose you don't), try a singleton pattern. Enum singleton (Singleton of Bloch) choose is better. BTW, I suppose you've just need an enum.
– Victor Gubin
Nov 20 at 16:45






You'd better not use any collection as a static field, collections are mutable i.e. collie class able to modify internal state. If you really need a collection (I suppose you don't), try a singleton pattern. Enum singleton (Singleton of Bloch) choose is better. BTW, I suppose you've just need an enum.
– Victor Gubin
Nov 20 at 16:45














2 Answers
2






active

oldest

votes


















0














You'd add this to the list; this being a reference to the current object (a Room in this case) being worked on:



listOfRooms.add(this);


Note though, having listOfRooms as a static member of the class is a bad idea. With this setup, you can only ever have one list, and anyone can alter the list however they want since it's public.



It would likely be much better to create something like a Hotel class and make it a member of that:



class Hotel {
ArrayList<Rooms> listOfRooms = new ArrayList<Rooms>();
}


Now you can at least have multiple hotels if necessary with separate lists of rooms, and to modify the rooms, code would at least require a reference to the hotel.






share|improve this answer































    0














    Adding to what Carcigenicate said, it's always better to have more abstraction in object oriented programming.
    For example, Hotel object has a Floor ArrayList in it. Each Floor object contains ArrayList of Room and so on.






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397555%2freferencing-an-object-inside-an-object%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      You'd add this to the list; this being a reference to the current object (a Room in this case) being worked on:



      listOfRooms.add(this);


      Note though, having listOfRooms as a static member of the class is a bad idea. With this setup, you can only ever have one list, and anyone can alter the list however they want since it's public.



      It would likely be much better to create something like a Hotel class and make it a member of that:



      class Hotel {
      ArrayList<Rooms> listOfRooms = new ArrayList<Rooms>();
      }


      Now you can at least have multiple hotels if necessary with separate lists of rooms, and to modify the rooms, code would at least require a reference to the hotel.






      share|improve this answer




























        0














        You'd add this to the list; this being a reference to the current object (a Room in this case) being worked on:



        listOfRooms.add(this);


        Note though, having listOfRooms as a static member of the class is a bad idea. With this setup, you can only ever have one list, and anyone can alter the list however they want since it's public.



        It would likely be much better to create something like a Hotel class and make it a member of that:



        class Hotel {
        ArrayList<Rooms> listOfRooms = new ArrayList<Rooms>();
        }


        Now you can at least have multiple hotels if necessary with separate lists of rooms, and to modify the rooms, code would at least require a reference to the hotel.






        share|improve this answer


























          0












          0








          0






          You'd add this to the list; this being a reference to the current object (a Room in this case) being worked on:



          listOfRooms.add(this);


          Note though, having listOfRooms as a static member of the class is a bad idea. With this setup, you can only ever have one list, and anyone can alter the list however they want since it's public.



          It would likely be much better to create something like a Hotel class and make it a member of that:



          class Hotel {
          ArrayList<Rooms> listOfRooms = new ArrayList<Rooms>();
          }


          Now you can at least have multiple hotels if necessary with separate lists of rooms, and to modify the rooms, code would at least require a reference to the hotel.






          share|improve this answer














          You'd add this to the list; this being a reference to the current object (a Room in this case) being worked on:



          listOfRooms.add(this);


          Note though, having listOfRooms as a static member of the class is a bad idea. With this setup, you can only ever have one list, and anyone can alter the list however they want since it's public.



          It would likely be much better to create something like a Hotel class and make it a member of that:



          class Hotel {
          ArrayList<Rooms> listOfRooms = new ArrayList<Rooms>();
          }


          Now you can at least have multiple hotels if necessary with separate lists of rooms, and to modify the rooms, code would at least require a reference to the hotel.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 at 16:55

























          answered Nov 20 at 16:45









          Carcigenicate

          17.2k43058




          17.2k43058

























              0














              Adding to what Carcigenicate said, it's always better to have more abstraction in object oriented programming.
              For example, Hotel object has a Floor ArrayList in it. Each Floor object contains ArrayList of Room and so on.






              share|improve this answer


























                0














                Adding to what Carcigenicate said, it's always better to have more abstraction in object oriented programming.
                For example, Hotel object has a Floor ArrayList in it. Each Floor object contains ArrayList of Room and so on.






                share|improve this answer
























                  0












                  0








                  0






                  Adding to what Carcigenicate said, it's always better to have more abstraction in object oriented programming.
                  For example, Hotel object has a Floor ArrayList in it. Each Floor object contains ArrayList of Room and so on.






                  share|improve this answer












                  Adding to what Carcigenicate said, it's always better to have more abstraction in object oriented programming.
                  For example, Hotel object has a Floor ArrayList in it. Each Floor object contains ArrayList of Room and so on.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 at 16:51









                  Bhhargava Choppakatla

                  84




                  84






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397555%2freferencing-an-object-inside-an-object%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Costa Masnaga

                      Fotorealismo

                      Sidney Franklin