• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Instantiating ArrayList From Within Constructor Parameter

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What would you be doing with these objects? Printing, sorting, searching, ...?
 
Andrew Linton Bradford
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks anyway.

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic