-1

In the below code, will the cahnges made to the Boolean value modifyPerson within the function be maintained or will it change to its initial value each time a new function is called.

Also i would like to know what difference is there if i use primitive boolean modifyPerson instead of Boolean .

public void validatePersonDTO(PersonDTO personDTO, TransactionLogDTO logDTO,ArrayList regionIdList,Boolean modifyPerson) {
    try {

        validateEffectiveIn(personDTO, logDTO,modifyPerson);

        validateCdsId(personDTO, logDTO,regionIdList,modifyPerson);

        validateEmpFirstName(personDTO, logDTO);

        validateEmpLastName(personDTO, logDTO);

        validateFinDept(personDTO, logDTO,modifyPerson);

        validateEmployeeClass(personDTO, logDTO,modifyPerson);

        validateWorkLoadandBudgetFTE(personDTO, logDTO,modifyPerson);

        validateStatusandDescription(personDTO, logDTO,modifyPerson);

        validateSalGrade(personDTO, logDTO);

        validateCostRate(personDTO, logDTO);

        validateJobCode(personDTO, logDTO,modifyPerson);

        validateRateCardCharged(personDTO, logDTO);

        validateSupervisorId(personDTO, logDTO);

    }catch (Exception e) {
        logDTO.setGyr("R");
        logDTO.setMessage(logDTO.getMessage()+";"+"PersonDTO could not be validated");
        //LOGGER.error("personDTO could not be validated: " + personDTO, e);
    }
}


protected void validateEffectiveIn(PersonDTO personDTO, TransactionLogDTO logDTO,boolean modifyPerson) throws Exception{



    todaysDate=convStringToDate(now(),Constants.DATE_PATTERN_LONG);

    if(effIn.after(todaysDate)){
        modifyPerson=true;
        logDTO.setGyr("R");
        logDTO.setMessage(logDTO.getMessage()+";"+"Error in Effective In date "+effIn.toString()+"cannot be greater than today’s date");
        throw new Exception ("Error in Effective In date "+effIn.toString()+"cannot be greater than today’s date");
    }

    else{
    modifyPerson=false;
    }
}
ashwinsakthi
  • 1,856
  • 4
  • 27
  • 56

4 Answers4

2

No, it won't propagate, and using the primitive won't help either.

The simplest way around this is to return the updated boolean value, and to assign the result of the method call back to the variable.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
2

Primitive variables are passed by value, so the changes to the modifyPerson parameter will be lost as soon as the method exits.

If you would like to preserve the change, return boolean from your method, and assign it to the modifyPerson, like this:

boolean validateEffectiveIn(PersonDTO personDTO, TransactionLogDTO logDTO) {
    ...
    return modifyPerson; // true or false
}

...
modifyPerson = false;
modifyPerson |= validateEffectiveIn(person1DTO, logDTO);
modifyPerson |= validateEffectiveIn(person2DTO, logDTO);

You can also create your own mutable class that encapsulates a boolean, and lets its users get and set its value.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

Java is pass by value. This means any primitive or reference (as you have here) passed has a local copy and altering it will not change the value held by the caller.

As you are throwing an exception after setting this boolean, you don't need it.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

I have used

boolean validateEffectiveIn(PersonDTO personDTO, TransactionLogDTO logDTO) {
return     modifyPerson; // true or false }  .

Thank You all for the help...

ashwinsakthi
  • 1,856
  • 4
  • 27
  • 56