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

  • SEARCH
  • Home
  • 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 8947819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:51:51+00:00 2026-06-15T12:51:51+00:00

I encountered this problem several times, so I m asking here what is the

  • 0

I encountered this problem several times, so I m asking here what is the best practice

I have process that goes true several objects, modifies them and rises some flags.

    boolean erros = false;
    for(MyData data: allData){
    //...

        //@ToDo  move this to a function titleCheck()
        if(data.getTitle().isEmpty()){
            data.setTitle('none');
            erros = true;
        }

        if(data.getTitle().equals('BIG')){
            data.setTitle('small')
        }

        if(data.getTitle().equals('error')){
            errors = true;
        }

    //...
    }

I need a function to update the data and change one or more flags
so the preferred syntax is something like this:

MyData updatedData = titleCheck(MyData data, &erros){...}

but booleans cant be passed by reference so they was it works is:

boolean errors = titleCheck(MyData dataForUpdate){...}

Witch is way less intuitive .. (at least for me … coming from PHP background)
The problem gets bigger if you have several flags to update.

So .. what is the proper Java way to handle such structure.

  • 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-15T12:51:52+00:00Added an answer on June 15, 2026 at 12:51 pm

    When you do this:

    titleCheck(MyData data);
    

    given your code above, you’re actually changing the data object – not a copy of this. Hence you can return the success/failure boolean and you don’t have to return the passed object. So now you can OR these values together.

    boolean error = false;
    error |= titleCheck(...);
    error |= nameCheck(...);
    

    etc.

    If you want to pass the object and a status back, you can trivially declare a return class thus:

    class ReturnResult {
       public boolean error;
       public MyData data;
    }
    

    (I’ve made the fields public since I’m using it as a simple structure). The advantage of this approach is that you’re making use of OO to tie together related objects, and you can put methods on the above class to make it easy to merge successive results.

    Finally I note that you’re calling lots of methods on the MyData object and determining success/failure outside that object. I would rather put this within the MyData object. Then the MyData object can store its own valid/invalid state and you can ask it for its own state via an isValid() method or similar. This seems like a very OO method of doing things here.

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

Sidebar

Related Questions

This is a problem I have encountered several times over the years, and I
I'm studying for upcoming interviews and have encountered this question several times (written verbatim)
I have encountered this problem a few times, and am not able to figure
I have encountered this bug several times. I have a application with a navigation
I have encountered this problem today and I don't have an explanation for it.
Hey this is the weirdest problem i've ever encoutered, I have 2 projects that's
I have encountered with this problem often. Couldn't think of a way to how
I'm working on a practice problem set for C programming, and I've encountered this
I've encountered this problem (while trying to add SQL Server Database (.mdf) file to
Hi i encountered this problem whereby when i initialized my String[], there seems to

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.