Doesn't it make sense to necessarily initialize references to some object to avoid NullPointerExceptions ? Can this be done ?
Javasearch.buggybread.com

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Interview Questions and Answers

 Q1. Doesn't it make sense to necessarily initialize references to some object to avoid NullPointerExceptions ? Can this be done ?Core Java
Ans. We can declare the reference as final to avoid reassignment but again we can always initialize the final reference to null. Even if there was any such facility available , it would have meant poor use of resources by assigning a new object in memory to each reference that's created. Many a times references are just meant to refer to other objects which already have a reference i.e sharing object by multiple references.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     null  NullPointerException


Related Questions

  When does an application throw NullPointerException ?
  Which of the following exception is thrown when we try to access element which is beyond the size ?
 Is it ok to use optional everywhere just to get over nullpointerexception ?
  How would you go about debugging a NullPointerException?
  How can we protect an application from throwing a NullPointerException ?
  Invoking start twice on same thread leads to ..
 Is it a bad practice to initialize object reference to Null ?



Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: