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 the 'new' initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "the Watch "the New topic
Author

the 'new' initialization

chad stevens
Ranch Hand

Joined: Aug 20, 2002
Posts: 88
Hi. I am a little confused about this basic question. Sometimes I see List theList = new ArrayList(); and sometimes I see List theList and then the list is getting values from somewhere without the new.. example

compared to:

What is the difference and when should we use the new and when is it just a waste of memory? Thanks.
CHAD
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
What is the difference and when should we use the new and when is it just a waste of memory? Thanks.

The difference is that when you use new you create a, well, new unique instance of some class, whereas in the other case the method returns an already existing instance. When is it a waste of memory to create new instances? That's really a very subjective question which depends on a lot of factors. Should you create an array filled with a hundred or more List objects? Probably not. But in general if you have a use for a new instance in your program then you should not be concerned about creating it. Some objects like threads or database connections which use a lot of system resources, you should certainly think twice about. That's why you will see terms like "thread pooling" in which you create a finite number of threads and use whichever one is inactive upon a client request.
[ May 23, 2003: Message edited by: Michael Morris ]

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Gary McGath
Ranch Hand

Joined: Mar 15, 2003
Posts: 52
In the examples you posted, the second one would be a clear waste of memory. You would be creating an instance of List and then immediately overwriting it with the value returned from getVals(). The first List would eventually have to be garbage collected, consuming processing resources.


http://www.mcgath.com/consulting/
chad stevens
Ranch Hand

Joined: Aug 20, 2002
Posts: 88
Thanks Gary,
That's pretty clear now, I suppose that you would only create a new instance when you will create and put new elements in, although, going through other people's code it seems to be quite common to see those 'wasted' assignments.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: the 'new' initialization
 
Similar Threads
dynamically determining an object instance?
i don't know what i'm doing
List to ArrayList
2 nd page / Next link - ' Nothing found to display' in DisplayTag
Model Attribute not present in destintation JSP