wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes What is the difference between these two List intiializations? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the difference between these two List intiializations?" Watch "What is the difference between these two List intiializations?" New topic
Author

What is the difference between these two List intiializations?

Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

What is the difference between these two Lists?

and

I can use the .add method on each right?

I do not understand the difference between the two, or what kind of functionality I gain or lose when making the choice.
Can someone help me or direct me to some reading that will enlighten me?

Thanks


True wisdom is in knowing you know nothing - Socrates
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

The first one is an empty reference - a null reference if you will. So no, you can't call orderList.add(order); on it because you would get a NullPointerException, (unless you assign an object to it between the variable definition and method call).

The second one actually creates an Object of a suitable, concrete implementation of List, and so can have a method called on it without an error occurring.


Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the difference between these two List intiializations?