0
import java.util.Optional;

public class Student {
   public long id;
   public String firstName;
   public Optional<String> middleName;
   public String lastName;
   .
   .
   .
   public Optional<String> getMiddleName() {
     return middleName;
   }
}

Student student = new Student();
if (student.getMiddleName().isPresent()) {
   System.out.println(student.getMiddleName().get());
}

The above code will get an NullPointerException since the default value of an Optional class member value is null. Should it be that the default value is Optional.empty()?

codenut
  • 211
  • 3
  • 8
  • 2
    Not sure why this is marked as a duplicate of http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it. This question mentions a `NullPointerException` but it is completely different -- it is basically a misconception of what `Optional` is and/or the default value for all reference types. – Grodriguez Mar 16 '17 at 07:54
  • Exactly. This is a different question. Do you know how to unmark it as duplicate? @Grodriguez – codenut Mar 16 '17 at 07:58
  • It will be reopened if it gets 5 reopen votes (voting for reopening requires some rep). I already voted for reopening. – Grodriguez Mar 16 '17 at 07:59
  • 1
    I agree that was a bad dupe, I've removed it and added a more appropriate one. *All* member variables are initialized with their default values if they are not provided with an explicit value; this is null in the case of a reference type (and zero in the case of numeric primitives, and false for booleans). `Optional` is a reference type like any other, there's nothing special about it. – Andy Turner Mar 16 '17 at 08:01
  • thanks @Grodriguez – codenut Mar 16 '17 at 08:04
  • ([Relevant section of language spec](https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.12.5)) – Andy Turner Mar 16 '17 at 08:12
  • @AndyTurner That one got much closer but I would still say this is a different question. I think the key issue here is failing to realize that an `Optional` is just like any other reference type (and not what specific value are reference types initialised to) – Grodriguez Mar 16 '17 at 08:19
  • @Grodriguez so are you suggesting we should have a question for each of `Optional` is null, `String` is null, `Integer` is null...? The comments (and answers) here make it clear that `Optional` is not special, so a generally applicable dupe is appropriate. – Andy Turner Mar 16 '17 at 08:20
  • 2
    @AndyTurner No, I'm not suggesting that. I am saying that the key point in this question is that the OP did not realize that `Optional` is, *despite the name*, just like any other reference type. Indeed the answers here make it clear that `Optional` is not special -- and these are the right answers for this question. As you can see, these answers would not apply to the question you linked to. The fact that the answers for these two questions are different should be enough evidence that they are not duplicates. – Grodriguez Mar 16 '17 at 08:23
  • @Grodriguez is correct. The question is specific to Optional because I am under the impression that the purpose of Optional is to safely handle null values but Optional itself can be null since it is a reference type. – codenut Mar 16 '17 at 08:36
  • @Grodriguez: Andy Turner’s question still holds. So when it makes a difference that the OP thinks that a class name has an impact on the variable, should we have a separate questions for `Optional`, `ThreadLocal`, `AtomicInteger`, `int[]`, `Random` etc. to emphasize that a reference variable is a reference variable, regardless of how the class has been named? – Holger Mar 16 '17 at 10:06
  • 1
    @Grodriguez: People thinking that the type name `Optional` turns a variable automagically into a non-`null` variable would also think that the type name `ThreadLocal` would turn the variable into something thread local, instead of just pointing to an object. – Holger Mar 16 '17 at 11:27
  • @Holger `ThreadLocal` -- really? Do you think that would actually happen? Anyway I already stated my position and there is no point in continuing with this argument. Just do as you wish. – Grodriguez Mar 16 '17 at 11:37
  • @Grodriguez: well, I didn’t believe people would think, naming a type `Optional` magically changes the nature of a reference variable, either. But you’re right, everything has been said. – Holger Mar 16 '17 at 11:41

2 Answers2

6

No, it shouldn't. Optional is a reference type like any other, and is defaulted to null by the same rules. If you want to initialize it to empty, you must do so explicitly:

public Optional<String> middleName = Optional.empty();
shmosel
  • 49,289
  • 6
  • 73
  • 138
4

The default value of any reference type member variable is null. The Optional type behaves as any other reference type.

Eran
  • 387,369
  • 54
  • 702
  • 768