Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

initialization collection in entity class

 
manu chaox
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using this initialization of collection:



but is it the best practice ? Why not following ?


 
Pawan Kr Gupta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear The second code will not compile.
This will give you a compile time error - incompatible types error , required:java.util.List<javax.swing.Document> found :java.util.HashSet
 
manu chaox
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawan Kr Gupta wrote:Dear The second code will not compile.
This will give you a compile time error - incompatible types error , required:java.util.List<javax.swing.Document> found :java.util.HashSet


Sorry, my mistake. I have edited it. Thank you for reply.
But the question remains.
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both are valid.

In general lazy initialization is more efficient, as you will not incur the cost of allocating the list unless you use it.

However, lazy initialize is more prone to null-pointers, as you must ensure you use the get method, not access the variable directly.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic