• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructor with arraylist?  RSS feed

 
Ruth Jordan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • 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.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!