• Post Reply Bookmark Topic Watch Topic
  • New Topic

Instantiating ArrayList From Within Constructor Parameter  RSS feed

 
Andrew Linton Bradford
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

While learning Java, I have started a small project.  I have run into trouble creating a Backpack class which can hold various types of objects.

The problem arises when I try to create a backpack object.



Here is how I am trying to instantiate it from the class Launcher:


I'm not really sure how to instantiate the ArrayList as a parameter in the constructor.

Any advice/corrections would be appreciated.

Thank you,
 
Carey Brown
Saloon Keeper
Posts: 3327
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your class has a bagspace list, what is it supposed to hold?
Normally you don't have a setter for a private list. What is your scenario that requires you to be able to set your internal list to a list that is passed in?
Having a List<Object> is a bad idea. What is the list supposed to hold. If you have a class BagSpace then this might be a List<BagSpace>.
 
Andrew Linton Bradford
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Carey,

I want the Backpack to be able to store InanimateObjects, but not really be an array or ArrayList, just possess one.

Bagspace was my way of trying to provide Backpack with a way of storing objects.

Thank you for your reply.



 
Carey Brown
Saloon Keeper
Posts: 3327
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would you be doing with these objects? Printing, sorting, searching, ...?
 
Andrew Linton Bradford
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks anyway.

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are size, weight, and bagspace declared as static members? If you do that, then all instances will share the same values and each new instance created will overwrite the values set by the last instance. I doubt that's what you want. Remove the static modifier to make these instance members instead, so each instance has its own size, weight, and space.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Also, you might have instantiated bagspace once, but you re‑assign it every time you create a new BackPack instance. What you are doing in the constructor is creating a new List containing the elements of the previous List, which looks a good thing to do. It is similar to No 3 in my list in this post, which tells you definitively I know of four ways to return a List. Since the introduction of the of() method in List in Java9, there is a fifth way to return a List.

But, as you have already been told, you don't want any of those fields being static. Also, what do size weight and broken refer to? Do they refer to the individual items or the whole backpack?

My favoured option for writing a class is to give it one constructor taking enough parameters to instantiate every field, and initialise every field in that constructor.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!