Optional return type of getter() in Java 8

0 votes

In Java 8, is it a good practice if I return an optional<foo>type instead of a classic foo using a getter method in Java?

Oct 23, 2018 in Java by anto.trigg4
• 3,420 points
38 views

1 answer to this question.

0 votes

Well, you can do that but originally this function is not supposed to be used as a general purpose Maybe type. According to the documentation, this function provides a limited mechanism for library method return types to represent "no result" in a clearer approach. Moreover, you can use this function as a return value for getters() but it might be considered as over-use of the function.

I think routinely using it as a return value for getters would definitely be over-use.

But according to the Public service announcement: " NEVER call Optional.get unless you can prove it will never be null; instead, use one of the safe methods like orElse or ifPresent. In retrospect, we should have called get something like getOrElseThrowNoSuchElementException or something that made it far clearer that this was a highly dangerous method that undermined the whole purpose of Optional in the first place. "

answered Oct 23, 2018 by code.reaper12
• 3,450 points

Related Questions In Java

0 votes
2 answers

One line initialization of an ArrayList object in Java

In Java 8 or earlier: List<String> string = ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,190 points
145 views
0 votes
2 answers

Counting no of Occurrence of a particular character inside a string in Java

We can find out the no. of ...READ MORE

answered Sep 7, 2018 in Java by Sushmita
• 6,880 points
256 views
0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,050 points
483 views
0 votes
1 answer

What are optional parameters in Java

Using three dots: public void move(Object... x) { ...READ MORE

answered Apr 27, 2018 in Java by developer_1
• 3,300 points
62 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,380 points

edited Nov 6, 2018 by Omkar 8,264 views
0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,090 points
2,005 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,090 points
5,233 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,090 points
512 views
0 votes
1 answer

Purpose of “String args[]” in the “psvm” of Java

Let me give you the complete explanation ...READ MORE

answered May 7, 2018 in Java by code.reaper12
• 3,450 points
87 views
0 votes
1 answer

“Missing return statement” within if / for / while in Java

Putting a return statement in any of the ...READ MORE

answered Sep 26, 2018 in Java by code.reaper12
• 3,450 points
1,220 views