aspose file tools*
The moose likes Beginning Java and the fly likes Adding to ArrayList in a Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adding to ArrayList in a Constructor" Watch "Adding to ArrayList in a Constructor" New topic
Author

Adding to ArrayList in a Constructor

Bobby Smallman
Ranch Hand

Joined: Sep 09, 2010
Posts: 107
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.


Everyday in every way, we get a little better.
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

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

Joined: Sep 09, 2010
Posts: 107
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

Joined: Aug 15, 2010
Posts: 230
    
    2

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
Soniya Ahuja
Ranch Hand

Joined: Jul 20, 2008
Posts: 83
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.


SCJP 1.5 | SCWCD 5 | SCJP 6.0
[url]http://a2zjava.webs.com[/url] - Online training for Java/JSPs and Servlets/SCJP/SCWCD
http://soniyaahuja.webs.com
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Bobby Smallman
Ranch Hand

Joined: Sep 09, 2010
Posts: 107
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
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Sep 09, 2010
Posts: 107
Perfect, seems all questions have been answered! Thank you to everyone who gave input!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
You're welcome
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Yesterday, I wrote:Please explain more . . .
Thank you

The edited post now includes that additional explanation.
Soniya Ahuja
Ranch Hand

Joined: Jul 20, 2008
Posts: 83
@Campbell
@Bobby - Welcome
Bobby Smallman
Ranch Hand

Joined: Sep 09, 2010
Posts: 107
Thanks for the welcome! I am excited to be spending a great deal of my time here!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Adding to ArrayList in a Constructor