Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8250691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:58:50+00:00 2026-06-07T23:58:50+00:00

I have a class private class TouchCommand { private int action; private int x;

  • 0

I have a class

private class TouchCommand {
  private int action;
  private int x;
  private int y;
...

When the command is executed, it is necessary to verify the field values ​​- null / not null, and depending on it to produce longitudinal action. I want to use Options from Google Guava.

Which solution is right?
this:

public boolean executeCommand() {
  Optional<Integer> optionalAction = Optional.fromNullable(action);
  ...

or:

private class TouchCommand {
  private Optional<Integer> action;
  private Optional<Integer> x;
  private Optional<Integer> y;
...

Given that the call to parseAction may also return a null (or absent):

TouchCommand touchCommand = new TouchCommand();
touchCommand.mAction = parseAction(xmlParser.getAttributeValue(namespace, "action"));
...

Questions:

  1. whether or not to do so: the method parseAction (and similar) returns Optional ?
  2. whether or not to do so: the field of class objects Optional ?
  3. whether or not to do so: when checking the fields of the class (assuming that they can be null) to convert them into objects Optional ?

Thx.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T23:58:52+00:00Added an answer on June 7, 2026 at 11:58 pm

    Guava contributor here…

    Any or all of these things are fine, but some of them may be overkill.

    Generally, as discussed in this StackOverflow answer, Optional is primarily used for two things: to make it clearer what you would’ve meant by null, and in method return values to make sure the caller takes care of the “absent” case (which it’s easier to forget with null). We certainly don’t advocate replacing every nullable value with an Optional everywhere in your code — we certainly don’t do that within Guava itself!

    A lot of this will have to be your decision — there’s no universal rule, it’s a relatively subjective judgement, and I don’t have enough context to determine what I’d do in your place — but based on what context you’ve provided, I’d consider making the methods return Optional, but probably wouldn’t change any of the other fields or anything.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
I have: class first{ private: int *array; public: first(int x){ array = new int[x][10];
I have a class like this: private static class Num { private int val;
I have the following class private static class Person{ private int iq; private Person[]
If I have a class as follows class Example_Class { private: int x; int
I have the following class: private class Info{ public String A; public int B;
I have this class: class DoSomething { private int timesDone; ... } Which is
I have a class: class MyClass { private var num : Int; } I
I have a class with a private field and suspected that the template could
i have class something like this class MClass { private int mem1,mem2,mem3.......; public int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.