File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to add an object to an array list in another class when the object is created? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to add an object to an array list in another class when the object is created?" Watch "How to add an object to an array list in another class when the object is created?" New topic
Author

How to add an object to an array list in another class when the object is created?

Jonathan Bjerche
Greenhorn

Joined: Nov 14, 2011
Posts: 9
I have 1 coupon class and 1 Coupon Register class that contains an array-list of coupons.

1 constructor that is something like this:

public Coupon (CouponRegister couponReg)
{
this.couponReg=couponReg;
couponReg.addCoupon(THIS##COUPON) <<< This is what I want to do, but HOW??
}

I want to add this instance of Coupon to the Array List in CouponRegister when the instance of Coupon is created, but how??
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6



Note, however, that it's generally bad practice to let a reference to "this" escape from the constructor before that constructor has completed. It can be done, but you have to be careful with it. You should be okay here, since I assume this is a pretty simple program with no multithreading, but it's something to be aware of.

Still, if it were me, rather than having the coupon register itself upon creation, I'd probably have the creator register the coupon. Maybe even use a factory method as the only way to create coupons, so that we'd know they'd always be registered once they were created. Again, though, this may be beyond your current level or needs.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Agree.
Use something likeEach class should be responsible for itself, not for other classes.

And welcome to the Ranch
Jonathan Bjerche
Greenhorn

Joined: Nov 14, 2011
Posts: 9
Thanks for the replies.

I suppose it's better to let my application-class add the coupon to the register when it creates a coupon then? (the application class is my class that interacts with the user)

It's a school project and Iam going to have a presentation where the teacher asks me why I have done it this way etc...
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jonathan Bjerche wrote:Thanks for the replies.

I suppose it's better to let my application-class add the coupon to the register when it creates a coupon then?


Yes, in general that would be the preferred approach, because 1) As a general rule of OO, a Coupon class should be responsible for what a Coupon is and does, not for keeping track of existing Coupons, and 2) In the Java language in particular, letting "this" escape from a constructor is dangerous and generally frowned upon.


It's a school project and Iam going to have a presentation where the teacher asks me why I have done it this way etc...


If you understand the above and can express them in your own words, you should have no problem with that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to add an object to an array list in another class when the object is created?