wood burning stoves*
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


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 "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: 3946
    
  17

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
 
Don't get me started about those stupid light bulbs.
 
subject: What is the difference between these two List intiializations?
 
Similar Threads
Hibernate 3 Annotations
about isThreadSafe
Using Generics method parameters and wildcards
Type Safety
Jsp Document vs Jsp Page - clarification