• 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

Constructor with arraylist?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

If I have a class with



Also, is this ok?

or this one is the correct one?


How should I create the constructor/s?

And only this? Or another constructor is necessary?

or should I write?


Thank very much in advance.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In most cases instance fields should be private and not public.
Unless 'venta' has to be an ArrayList and no other type of List I would recommend declaring it as a 'List' rather than as an 'ArrayList'. Declaring it as a List makes your code more flexible and easier to change if at a later stage you find a different List type collection is more appropriate.

The other questions are hard to answer without knowing what you are trying to achieve, please can you give more information. May I suggest you give information on what you need to achieve rather than how you think you should do it.
 
Ruth Jordan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it's the class ticket & in this class I have to include the date as a unique number, the products sold & the total amount. In a later stage, this will be part of an invoice & I will include the customers.
These tickets will give me the opportunity to manage my stock considering the sold products included en each of them.

Thanks.
Ruth.
 
Ruth Jordan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah! about the private -public, I have received errors because of the access of other classes that are in other packages, I thought it doesn't matter.
I didn¡t know how to solve that problem & that was the easiest way.
Thanks.
 
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

Ruth Jordan wrote:Ah! about the private -public, I have received errors because of the access of other classes that are in other packages, I thought it doesn't matter.
I didn¡t know how to solve that problem & that was the easiest way.
Thanks.


You need to supply a "getter" method.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you pass a List to the constructor, that is a mutable reference type, so changes in one place would be reflected elsewhere. Look for “defensive copies” and you find a few hits:- 1 2. So you would probably have to find some way of copying the List. If you look here, you find that you can use the same List, or you can use a read‑only version of it, or you can use a copy which you can modify.

… or you can even use a copy which you cannot modify.
 
WARNING! Do not activate jet boots indoors or you will see a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic