Referencing an Object Inside an Object
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
add a comment |
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
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
add a comment |
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
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
java class
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 20 at 16:55
answered Nov 20 at 16:45
Carcigenicate
17.2k43058
17.2k43058
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 20 at 16:51
Bhhargava Choppakatla
84
84
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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