Why no public constructor for Optional in java?
up vote
19
down vote
favorite
Why does Optional
have methods like of()
and ofNullable()
instead of a public constructor?
java java-8 optional
add a comment |
up vote
19
down vote
favorite
Why does Optional
have methods like of()
and ofNullable()
instead of a public constructor?
java java-8 optional
3
Because empty is a singleton for memory efficiency.
– Minn
Dec 2 at 11:51
8
Because how anOptional
is created is an implementation detail
– ZhekaKozlov
Dec 2 at 11:57
add a comment |
up vote
19
down vote
favorite
up vote
19
down vote
favorite
Why does Optional
have methods like of()
and ofNullable()
instead of a public constructor?
java java-8 optional
Why does Optional
have methods like of()
and ofNullable()
instead of a public constructor?
java java-8 optional
java java-8 optional
edited Dec 2 at 14:11
Boann
36.6k1287121
36.6k1287121
asked Dec 2 at 11:48
Gopal S Akshintala
1,0331714
1,0331714
3
Because empty is a singleton for memory efficiency.
– Minn
Dec 2 at 11:51
8
Because how anOptional
is created is an implementation detail
– ZhekaKozlov
Dec 2 at 11:57
add a comment |
3
Because empty is a singleton for memory efficiency.
– Minn
Dec 2 at 11:51
8
Because how anOptional
is created is an implementation detail
– ZhekaKozlov
Dec 2 at 11:57
3
3
Because empty is a singleton for memory efficiency.
– Minn
Dec 2 at 11:51
Because empty is a singleton for memory efficiency.
– Minn
Dec 2 at 11:51
8
8
Because how an
Optional
is created is an implementation detail– ZhekaKozlov
Dec 2 at 11:57
Because how an
Optional
is created is an implementation detail– ZhekaKozlov
Dec 2 at 11:57
add a comment |
4 Answers
4
active
oldest
votes
up vote
29
down vote
accepted
From Joshua Bloch effective Java, Chapter 2. Creating and Destroying
Objects, 1 Item:
Consider static factory methods instead of constructors
Why?
One advantage of static factory methods is that, unlike constructors,
they have names.
With static factory methods we can specify some instantiation behavior in the method definition. This makes the API easier to use and we prevent clients from calling wrong constructors.
For instance here: In Optional.ofNullable
-> we allow null value be passed to instantiate the Optional, in Optional.of
null value is not allowed and throw exception. We could not use the constructor here.
private Optional(T value) {
this.value = Objects.requireNonNull(value); //this throws NullPointerException
}
public static <T> Optional<T> of(T value) {
return new Optional<>(value);
}
public static <T> Optional<T> ofNullable(T value) {
return value == null ? empty() : of(value);
}
Another advantage (already mentioned):
A second advantage of static factory methods is that, unlike
constructors, they are not required to create a new object each time
they’re invoked.
In Optional, the empty value is instantiated just once, and then stored in the static field, this value is reused always when the program needs an empty value.
private static final Optional<?> EMPTY = new Optional<>(); //instantiate value when program starts
public static<T> Optional<T> empty() {
@SuppressWarnings("unchecked")
Optional<T> t = (Optional<T>) EMPTY; //return stored value when requested
return t;
}
6
And not to forget: the namesof
andofNullable
do not only document the different semantics of theses two factories, they also make them even possible, as we have two methods with identical type signatures, hence need different names to declare them in the same class. Since constructors don't have names, it is impossible to declare two of them with identical argument types.
– Holger
Dec 2 at 22:20
Of course he means that on the public API. I mean, obviously you need to have constructors eventually, it can't just be an endless chain of factory methods... or can it?!
– corsiKa
2 days ago
add a comment |
up vote
19
down vote
The reason is actually quite simple: an empty optional is a static constant to be more memory efficient. If a constructor were used, it would have to create a new instance every time for a common case.
public static<T> Optional<T> empty() {
@SuppressWarnings("unchecked")
Optional<T> t = (Optional<T>) EMPTY;
return t;
}
public static <T> Optional<T> ofNullable(T value) {
return value == null ? empty() : of(value);
}
Having a public constructor doesn't preclude having a static method that returns a singleton instance.
– Chris Cooper
2 days ago
I have not said that would be the case, I only answered the question.
– Minn
2 days ago
"If a constructor were used, it would have to create a new instance every time for a common case." - the way this is worded to me implies that you would have to use the constructor rather than use the factory method. Perhaps you could reword it to be clear that having a public constructor would mean people would be more likely to create duplicate empty instances?
– Chris Cooper
2 days ago
add a comment |
up vote
10
down vote
Optional is a Value-based Class without any constructors
do not have accessible constructors, but are instead instantiated through factory methods which make no committment as to the identity of returned instances
14
The OP knows that. He's asking why that is.
– JB Nizet
Dec 2 at 11:53
1
@JBNizet Do you think the OP actually asked: why value-based classes have no accessible constructors?
– Eugen Covaci
Dec 2 at 12:25
13
No. I think he asked why the designers of Optional chose not to add a public constructor to Optional. Answering that Optional is a value class and thus doesn't have a public constructor doesn't answer the question, IMHO.
– JB Nizet
Dec 2 at 12:31
2
@JBNizet: I think it does answer the question technically, but opens other questions: "What is a value class", and "What are the advantages of a value class". This answer would be better if it also contained those bigs of information.
– Mooing Duck
Dec 2 at 20:08
add a comment |
up vote
8
down vote
Because factory methods should be favored over public constructors when the possible instantiation cases are known.
It makes the API easier to use for client classes.
Besides factory methods allow to decide whether an instance should be created at each invocation.
In the case of Optional.empty()
it makes sense to cache the value as that is immutable.
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
29
down vote
accepted
From Joshua Bloch effective Java, Chapter 2. Creating and Destroying
Objects, 1 Item:
Consider static factory methods instead of constructors
Why?
One advantage of static factory methods is that, unlike constructors,
they have names.
With static factory methods we can specify some instantiation behavior in the method definition. This makes the API easier to use and we prevent clients from calling wrong constructors.
For instance here: In Optional.ofNullable
-> we allow null value be passed to instantiate the Optional, in Optional.of
null value is not allowed and throw exception. We could not use the constructor here.
private Optional(T value) {
this.value = Objects.requireNonNull(value); //this throws NullPointerException
}
public static <T> Optional<T> of(T value) {
return new Optional<>(value);
}
public static <T> Optional<T> ofNullable(T value) {
return value == null ? empty() : of(value);
}
Another advantage (already mentioned):
A second advantage of static factory methods is that, unlike
constructors, they are not required to create a new object each time
they’re invoked.
In Optional, the empty value is instantiated just once, and then stored in the static field, this value is reused always when the program needs an empty value.
private static final Optional<?> EMPTY = new Optional<>(); //instantiate value when program starts
public static<T> Optional<T> empty() {
@SuppressWarnings("unchecked")
Optional<T> t = (Optional<T>) EMPTY; //return stored value when requested
return t;
}
6
And not to forget: the namesof
andofNullable
do not only document the different semantics of theses two factories, they also make them even possible, as we have two methods with identical type signatures, hence need different names to declare them in the same class. Since constructors don't have names, it is impossible to declare two of them with identical argument types.
– Holger
Dec 2 at 22:20
Of course he means that on the public API. I mean, obviously you need to have constructors eventually, it can't just be an endless chain of factory methods... or can it?!
– corsiKa
2 days ago
add a comment |
up vote
29
down vote
accepted
From Joshua Bloch effective Java, Chapter 2. Creating and Destroying
Objects, 1 Item:
Consider static factory methods instead of constructors
Why?
One advantage of static factory methods is that, unlike constructors,
they have names.
With static factory methods we can specify some instantiation behavior in the method definition. This makes the API easier to use and we prevent clients from calling wrong constructors.
For instance here: In Optional.ofNullable
-> we allow null value be passed to instantiate the Optional, in Optional.of
null value is not allowed and throw exception. We could not use the constructor here.
private Optional(T value) {
this.value = Objects.requireNonNull(value); //this throws NullPointerException
}
public static <T> Optional<T> of(T value) {
return new Optional<>(value);
}
public static <T> Optional<T> ofNullable(T value) {
return value == null ? empty() : of(value);
}
Another advantage (already mentioned):
A second advantage of static factory methods is that, unlike
constructors, they are not required to create a new object each time
they’re invoked.
In Optional, the empty value is instantiated just once, and then stored in the static field, this value is reused always when the program needs an empty value.
private static final Optional<?> EMPTY = new Optional<>(); //instantiate value when program starts
public static<T> Optional<T> empty() {
@SuppressWarnings("unchecked")
Optional<T> t = (Optional<T>) EMPTY; //return stored value when requested
return t;
}
6
And not to forget: the namesof
andofNullable
do not only document the different semantics of theses two factories, they also make them even possible, as we have two methods with identical type signatures, hence need different names to declare them in the same class. Since constructors don't have names, it is impossible to declare two of them with identical argument types.
– Holger
Dec 2 at 22:20
Of course he means that on the public API. I mean, obviously you need to have constructors eventually, it can't just be an endless chain of factory methods... or can it?!
– corsiKa
2 days ago
add a comment |
up vote
29
down vote
accepted
up vote
29
down vote
accepted
From Joshua Bloch effective Java, Chapter 2. Creating and Destroying
Objects, 1 Item:
Consider static factory methods instead of constructors
Why?
One advantage of static factory methods is that, unlike constructors,
they have names.
With static factory methods we can specify some instantiation behavior in the method definition. This makes the API easier to use and we prevent clients from calling wrong constructors.
For instance here: In Optional.ofNullable
-> we allow null value be passed to instantiate the Optional, in Optional.of
null value is not allowed and throw exception. We could not use the constructor here.
private Optional(T value) {
this.value = Objects.requireNonNull(value); //this throws NullPointerException
}
public static <T> Optional<T> of(T value) {
return new Optional<>(value);
}
public static <T> Optional<T> ofNullable(T value) {
return value == null ? empty() : of(value);
}
Another advantage (already mentioned):
A second advantage of static factory methods is that, unlike
constructors, they are not required to create a new object each time
they’re invoked.
In Optional, the empty value is instantiated just once, and then stored in the static field, this value is reused always when the program needs an empty value.
private static final Optional<?> EMPTY = new Optional<>(); //instantiate value when program starts
public static<T> Optional<T> empty() {
@SuppressWarnings("unchecked")
Optional<T> t = (Optional<T>) EMPTY; //return stored value when requested
return t;
}
From Joshua Bloch effective Java, Chapter 2. Creating and Destroying
Objects, 1 Item:
Consider static factory methods instead of constructors
Why?
One advantage of static factory methods is that, unlike constructors,
they have names.
With static factory methods we can specify some instantiation behavior in the method definition. This makes the API easier to use and we prevent clients from calling wrong constructors.
For instance here: In Optional.ofNullable
-> we allow null value be passed to instantiate the Optional, in Optional.of
null value is not allowed and throw exception. We could not use the constructor here.
private Optional(T value) {
this.value = Objects.requireNonNull(value); //this throws NullPointerException
}
public static <T> Optional<T> of(T value) {
return new Optional<>(value);
}
public static <T> Optional<T> ofNullable(T value) {
return value == null ? empty() : of(value);
}
Another advantage (already mentioned):
A second advantage of static factory methods is that, unlike
constructors, they are not required to create a new object each time
they’re invoked.
In Optional, the empty value is instantiated just once, and then stored in the static field, this value is reused always when the program needs an empty value.
private static final Optional<?> EMPTY = new Optional<>(); //instantiate value when program starts
public static<T> Optional<T> empty() {
@SuppressWarnings("unchecked")
Optional<T> t = (Optional<T>) EMPTY; //return stored value when requested
return t;
}
edited 2 days ago
Bakudan
13.3k84264
13.3k84264
answered Dec 2 at 13:21
Stefan Repcek
1,16211122
1,16211122
6
And not to forget: the namesof
andofNullable
do not only document the different semantics of theses two factories, they also make them even possible, as we have two methods with identical type signatures, hence need different names to declare them in the same class. Since constructors don't have names, it is impossible to declare two of them with identical argument types.
– Holger
Dec 2 at 22:20
Of course he means that on the public API. I mean, obviously you need to have constructors eventually, it can't just be an endless chain of factory methods... or can it?!
– corsiKa
2 days ago
add a comment |
6
And not to forget: the namesof
andofNullable
do not only document the different semantics of theses two factories, they also make them even possible, as we have two methods with identical type signatures, hence need different names to declare them in the same class. Since constructors don't have names, it is impossible to declare two of them with identical argument types.
– Holger
Dec 2 at 22:20
Of course he means that on the public API. I mean, obviously you need to have constructors eventually, it can't just be an endless chain of factory methods... or can it?!
– corsiKa
2 days ago
6
6
And not to forget: the names
of
and ofNullable
do not only document the different semantics of theses two factories, they also make them even possible, as we have two methods with identical type signatures, hence need different names to declare them in the same class. Since constructors don't have names, it is impossible to declare two of them with identical argument types.– Holger
Dec 2 at 22:20
And not to forget: the names
of
and ofNullable
do not only document the different semantics of theses two factories, they also make them even possible, as we have two methods with identical type signatures, hence need different names to declare them in the same class. Since constructors don't have names, it is impossible to declare two of them with identical argument types.– Holger
Dec 2 at 22:20
Of course he means that on the public API. I mean, obviously you need to have constructors eventually, it can't just be an endless chain of factory methods... or can it?!
– corsiKa
2 days ago
Of course he means that on the public API. I mean, obviously you need to have constructors eventually, it can't just be an endless chain of factory methods... or can it?!
– corsiKa
2 days ago
add a comment |
up vote
19
down vote
The reason is actually quite simple: an empty optional is a static constant to be more memory efficient. If a constructor were used, it would have to create a new instance every time for a common case.
public static<T> Optional<T> empty() {
@SuppressWarnings("unchecked")
Optional<T> t = (Optional<T>) EMPTY;
return t;
}
public static <T> Optional<T> ofNullable(T value) {
return value == null ? empty() : of(value);
}
Having a public constructor doesn't preclude having a static method that returns a singleton instance.
– Chris Cooper
2 days ago
I have not said that would be the case, I only answered the question.
– Minn
2 days ago
"If a constructor were used, it would have to create a new instance every time for a common case." - the way this is worded to me implies that you would have to use the constructor rather than use the factory method. Perhaps you could reword it to be clear that having a public constructor would mean people would be more likely to create duplicate empty instances?
– Chris Cooper
2 days ago
add a comment |
up vote
19
down vote
The reason is actually quite simple: an empty optional is a static constant to be more memory efficient. If a constructor were used, it would have to create a new instance every time for a common case.
public static<T> Optional<T> empty() {
@SuppressWarnings("unchecked")
Optional<T> t = (Optional<T>) EMPTY;
return t;
}
public static <T> Optional<T> ofNullable(T value) {
return value == null ? empty() : of(value);
}
Having a public constructor doesn't preclude having a static method that returns a singleton instance.
– Chris Cooper
2 days ago
I have not said that would be the case, I only answered the question.
– Minn
2 days ago
"If a constructor were used, it would have to create a new instance every time for a common case." - the way this is worded to me implies that you would have to use the constructor rather than use the factory method. Perhaps you could reword it to be clear that having a public constructor would mean people would be more likely to create duplicate empty instances?
– Chris Cooper
2 days ago
add a comment |
up vote
19
down vote
up vote
19
down vote
The reason is actually quite simple: an empty optional is a static constant to be more memory efficient. If a constructor were used, it would have to create a new instance every time for a common case.
public static<T> Optional<T> empty() {
@SuppressWarnings("unchecked")
Optional<T> t = (Optional<T>) EMPTY;
return t;
}
public static <T> Optional<T> ofNullable(T value) {
return value == null ? empty() : of(value);
}
The reason is actually quite simple: an empty optional is a static constant to be more memory efficient. If a constructor were used, it would have to create a new instance every time for a common case.
public static<T> Optional<T> empty() {
@SuppressWarnings("unchecked")
Optional<T> t = (Optional<T>) EMPTY;
return t;
}
public static <T> Optional<T> ofNullable(T value) {
return value == null ? empty() : of(value);
}
edited Dec 2 at 23:48
Brian McCutchon
3,36021534
3,36021534
answered Dec 2 at 11:54
Minn
61513
61513
Having a public constructor doesn't preclude having a static method that returns a singleton instance.
– Chris Cooper
2 days ago
I have not said that would be the case, I only answered the question.
– Minn
2 days ago
"If a constructor were used, it would have to create a new instance every time for a common case." - the way this is worded to me implies that you would have to use the constructor rather than use the factory method. Perhaps you could reword it to be clear that having a public constructor would mean people would be more likely to create duplicate empty instances?
– Chris Cooper
2 days ago
add a comment |
Having a public constructor doesn't preclude having a static method that returns a singleton instance.
– Chris Cooper
2 days ago
I have not said that would be the case, I only answered the question.
– Minn
2 days ago
"If a constructor were used, it would have to create a new instance every time for a common case." - the way this is worded to me implies that you would have to use the constructor rather than use the factory method. Perhaps you could reword it to be clear that having a public constructor would mean people would be more likely to create duplicate empty instances?
– Chris Cooper
2 days ago
Having a public constructor doesn't preclude having a static method that returns a singleton instance.
– Chris Cooper
2 days ago
Having a public constructor doesn't preclude having a static method that returns a singleton instance.
– Chris Cooper
2 days ago
I have not said that would be the case, I only answered the question.
– Minn
2 days ago
I have not said that would be the case, I only answered the question.
– Minn
2 days ago
"If a constructor were used, it would have to create a new instance every time for a common case." - the way this is worded to me implies that you would have to use the constructor rather than use the factory method. Perhaps you could reword it to be clear that having a public constructor would mean people would be more likely to create duplicate empty instances?
– Chris Cooper
2 days ago
"If a constructor were used, it would have to create a new instance every time for a common case." - the way this is worded to me implies that you would have to use the constructor rather than use the factory method. Perhaps you could reword it to be clear that having a public constructor would mean people would be more likely to create duplicate empty instances?
– Chris Cooper
2 days ago
add a comment |
up vote
10
down vote
Optional is a Value-based Class without any constructors
do not have accessible constructors, but are instead instantiated through factory methods which make no committment as to the identity of returned instances
14
The OP knows that. He's asking why that is.
– JB Nizet
Dec 2 at 11:53
1
@JBNizet Do you think the OP actually asked: why value-based classes have no accessible constructors?
– Eugen Covaci
Dec 2 at 12:25
13
No. I think he asked why the designers of Optional chose not to add a public constructor to Optional. Answering that Optional is a value class and thus doesn't have a public constructor doesn't answer the question, IMHO.
– JB Nizet
Dec 2 at 12:31
2
@JBNizet: I think it does answer the question technically, but opens other questions: "What is a value class", and "What are the advantages of a value class". This answer would be better if it also contained those bigs of information.
– Mooing Duck
Dec 2 at 20:08
add a comment |
up vote
10
down vote
Optional is a Value-based Class without any constructors
do not have accessible constructors, but are instead instantiated through factory methods which make no committment as to the identity of returned instances
14
The OP knows that. He's asking why that is.
– JB Nizet
Dec 2 at 11:53
1
@JBNizet Do you think the OP actually asked: why value-based classes have no accessible constructors?
– Eugen Covaci
Dec 2 at 12:25
13
No. I think he asked why the designers of Optional chose not to add a public constructor to Optional. Answering that Optional is a value class and thus doesn't have a public constructor doesn't answer the question, IMHO.
– JB Nizet
Dec 2 at 12:31
2
@JBNizet: I think it does answer the question technically, but opens other questions: "What is a value class", and "What are the advantages of a value class". This answer would be better if it also contained those bigs of information.
– Mooing Duck
Dec 2 at 20:08
add a comment |
up vote
10
down vote
up vote
10
down vote
Optional is a Value-based Class without any constructors
do not have accessible constructors, but are instead instantiated through factory methods which make no committment as to the identity of returned instances
Optional is a Value-based Class without any constructors
do not have accessible constructors, but are instead instantiated through factory methods which make no committment as to the identity of returned instances
edited Dec 2 at 11:56
answered Dec 2 at 11:51
user7294900
19.3k93157
19.3k93157
14
The OP knows that. He's asking why that is.
– JB Nizet
Dec 2 at 11:53
1
@JBNizet Do you think the OP actually asked: why value-based classes have no accessible constructors?
– Eugen Covaci
Dec 2 at 12:25
13
No. I think he asked why the designers of Optional chose not to add a public constructor to Optional. Answering that Optional is a value class and thus doesn't have a public constructor doesn't answer the question, IMHO.
– JB Nizet
Dec 2 at 12:31
2
@JBNizet: I think it does answer the question technically, but opens other questions: "What is a value class", and "What are the advantages of a value class". This answer would be better if it also contained those bigs of information.
– Mooing Duck
Dec 2 at 20:08
add a comment |
14
The OP knows that. He's asking why that is.
– JB Nizet
Dec 2 at 11:53
1
@JBNizet Do you think the OP actually asked: why value-based classes have no accessible constructors?
– Eugen Covaci
Dec 2 at 12:25
13
No. I think he asked why the designers of Optional chose not to add a public constructor to Optional. Answering that Optional is a value class and thus doesn't have a public constructor doesn't answer the question, IMHO.
– JB Nizet
Dec 2 at 12:31
2
@JBNizet: I think it does answer the question technically, but opens other questions: "What is a value class", and "What are the advantages of a value class". This answer would be better if it also contained those bigs of information.
– Mooing Duck
Dec 2 at 20:08
14
14
The OP knows that. He's asking why that is.
– JB Nizet
Dec 2 at 11:53
The OP knows that. He's asking why that is.
– JB Nizet
Dec 2 at 11:53
1
1
@JBNizet Do you think the OP actually asked: why value-based classes have no accessible constructors?
– Eugen Covaci
Dec 2 at 12:25
@JBNizet Do you think the OP actually asked: why value-based classes have no accessible constructors?
– Eugen Covaci
Dec 2 at 12:25
13
13
No. I think he asked why the designers of Optional chose not to add a public constructor to Optional. Answering that Optional is a value class and thus doesn't have a public constructor doesn't answer the question, IMHO.
– JB Nizet
Dec 2 at 12:31
No. I think he asked why the designers of Optional chose not to add a public constructor to Optional. Answering that Optional is a value class and thus doesn't have a public constructor doesn't answer the question, IMHO.
– JB Nizet
Dec 2 at 12:31
2
2
@JBNizet: I think it does answer the question technically, but opens other questions: "What is a value class", and "What are the advantages of a value class". This answer would be better if it also contained those bigs of information.
– Mooing Duck
Dec 2 at 20:08
@JBNizet: I think it does answer the question technically, but opens other questions: "What is a value class", and "What are the advantages of a value class". This answer would be better if it also contained those bigs of information.
– Mooing Duck
Dec 2 at 20:08
add a comment |
up vote
8
down vote
Because factory methods should be favored over public constructors when the possible instantiation cases are known.
It makes the API easier to use for client classes.
Besides factory methods allow to decide whether an instance should be created at each invocation.
In the case of Optional.empty()
it makes sense to cache the value as that is immutable.
add a comment |
up vote
8
down vote
Because factory methods should be favored over public constructors when the possible instantiation cases are known.
It makes the API easier to use for client classes.
Besides factory methods allow to decide whether an instance should be created at each invocation.
In the case of Optional.empty()
it makes sense to cache the value as that is immutable.
add a comment |
up vote
8
down vote
up vote
8
down vote
Because factory methods should be favored over public constructors when the possible instantiation cases are known.
It makes the API easier to use for client classes.
Besides factory methods allow to decide whether an instance should be created at each invocation.
In the case of Optional.empty()
it makes sense to cache the value as that is immutable.
Because factory methods should be favored over public constructors when the possible instantiation cases are known.
It makes the API easier to use for client classes.
Besides factory methods allow to decide whether an instance should be created at each invocation.
In the case of Optional.empty()
it makes sense to cache the value as that is immutable.
edited 2 days ago
Bakudan
13.3k84264
13.3k84264
answered Dec 2 at 11:52
davidxxx
61.9k55685
61.9k55685
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53579911%2fwhy-no-public-constructor-for-optional-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Because empty is a singleton for memory efficiency.
– Minn
Dec 2 at 11:51
8
Because how an
Optional
is created is an implementation detail– ZhekaKozlov
Dec 2 at 11:57