• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the difference between these two List intiializations?

 
Matt Kohanek
Village Idiot
Ranch Hand
Posts: 484
Java jQuery Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic