• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Adding to ArrayList in a Constructor

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone!

I am trying to figure out if there is a way to add a newly created object to an ArrayList in a constructor. It seems that I cannot get it to work the way I would like because really the object is not yet created when it is trying to do it. Is it ever possible and if so is it ever done or is it not proper?









Those are the main() and the constructor which gets called where I would like to add the new objects to the ArrayList.
 
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bobby Smallman wrote:


You are passing dog0 as constructor parameter and assigning the object instance back to dog0.
What you are trying to do?
What is the problem you are trying to solve?
Shouldn't it be

Animal shouldn't know about the List, so it shouldn't pass the List as constructor parameter.
 
Bobby Smallman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can absolutely do that. But I was trying to make the adding of the animal object to the ArrayList in the constructor of the object to streamline the creation of the object. It isn't a big deal to do a animals.add(Animal) for each of the objects in main when there are only a couple of them, but wouldn't running it in the constructor streamline the process for when you have TONS of entries?
 
Raymond Tong
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bobby Smallman wrote:You can absolutely do that. But I was trying to make the adding of the animal object to the ArrayList in the constructor of the object to streamline the creation of the object. It isn't a big deal to do a animals.add(Animal) for each of the objects in main when there are only a couple of them, but wouldn't running it in the constructor streamline the process for when you have TONS of entries?


So you are trying to get rid of typing many animals.add(Animal)?
Let say I have an object called BankAccount, if I pass the bankAccountList to constructor ?
to expose the bankAccount details belong to other bank accounts?
Actually the code could compiles, but this shouldn't be a good design, right ?

If you want to reduce lines of code, you could do

Or you could do
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Bobby,

What are you trying to achieve? I think that what you are trying to do is somehow store all the animals created in an ArrayList? Is that correct?

If yes, then first of all, this ArrayList should belong to the class instead of instances of the class - so it should be marked as static.
Then from the constructor you can call arrayList.add(this)

Note:
A static member belongs to the class rather than a particular object. Each object has it's own copy of non-static objects - however, there is just one copy of a static member which is shared between all instances of that class. Further, a static member is associated with a class. So, existence of a static member depends only on the existence of a class and a static member is accessed as ClassName.staticMemberName. Thus, static members DO NOT need objects to access them.

this keyword refers to the current instance as already pointed by Ernest. You were trying to pass the animal reference but actually it was the object that you needed access to and using "this" you can access that instance.


Eg



Now when you create a new animal, it will automatically be added to the ArrayList animals. You can write more methods to access the ArrayList and you may also mark it as final to make sure that all instances access the same ArrayList and do not create a new one and assign that by mistake.

Again, the code above might not be what you are expecting because I haven't really understood your question or rather your purpose well.
 
Marshal
Posts: 80096
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain more about why you think a static List would be a good choice. I don't think you have given enough detail for people "beginning Java" to understand your solution.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bobby, I think Soniya hit the nail on the head by showing you a bit of Java knowledge you didn't previously have: the this keyword, which refers to the current object.
 
Bobby Smallman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you to everyone who responded. The animals.add(this) solution was what I was looking for. Is it not good practice to allow the constructor to add the new item to an ArrayList? Would a Static Factory (Item 1 of Effective Java) be more efficient?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the arraylist is supposed to contain all Animals ever created, then yes, that's a perfectly fine way to do it. In a real application there might be issues with garbage collection -- i.e., if every Animal is added to this list, then they can never be collected -- and in that case, something more explicit might be better, so that animals could be added or removed from the list.
 
Bobby Smallman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perfect, seems all questions have been answered! Thank you to everyone who gave input!
 
Campbell Ritchie
Marshal
Posts: 80096
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Campbell Ritchie
Marshal
Posts: 80096
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yesterday, I wrote:Please explain more . . .

Thank you

The edited post now includes that additional explanation.
 
Soniya Ahuja
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Campbell
@Bobby - Welcome
 
Bobby Smallman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the welcome! I am excited to be spending a great deal of my time here!
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic