• Post Reply Bookmark Topic Watch Topic
  • New Topic

Initializing the HashSet, which 1 is preferable?  RSS feed

 
Amandeep Singh
Ranch Hand
Posts: 853
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1st approach



2nd approach




3rd approach




Let me know which approach is most preferable.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see any difference between your first two.

In any case, the answer is "none of the above". HashSet's constructor doesn't throw any exceptions; even if it did, empty "catch" blocks are an extraordinarily bad practice.

For classes whose constructors do throw an exception, the choice between the two alternatives here should be made based on whether the variable needs to be accessible even if the constructor fails, in which case you should use the first alternative. Most of the time, the second alternative is better.
 
Amandeep Singh
Ranch Hand
Posts: 853
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:I don't see any difference between your first two.

In any case, the answer is "none of the above". HashSet's constructor doesn't throw any exceptions; even if it did, empty "catch" blocks are an extraordinarily bad practice.

For classes whose constructors do throw an exception, the choice between the two alternatives here should be made based on whether the variable needs to be accessible even if the constructor fails, in which case you should use the first alternative. Most of the time, the second alternative is better.


catch was used, just to show my example.

there is a difference between 1st approach and 2nd approach.
i.e
Set setty = null;

Set setty;
 
Ryan Beckett
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need catch blocks in either example. Example 3 is not advisable because you've restricted the scope of setty to the try/catch block.

You'll receive a compile-time error if you try to use a local variable without explicitly initializing it.




Ultimately, if your going to create a hashSet directly after you declare one, then why wouldn't you just do it in one line?

 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Beckett wrote:Example 3 is not advisable because you've restricted the scope of setty to the try/catch block.

Actually, unless you need the set outside of the try block, it is highly advisable to use example 3. By keeping variables in the smallest possible scopes, they are available for garbage collection sooner (when the try-block ends, not when the method ends). Also, it doesn't "polute" the current scope with variables you no longer need.

Of course, if you need the Set outside of the try-block, then example 3 isn't an option, as you already noticed.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!