1

I have an java application where a object reference "Validate.Options" is passed as parameter to the function "ValidateResult(Validate.Options option)" and the function is called iterative. Within this function based on the certain condition the property "enableProcessing" of the passed object gets changed which does not get reset on the next iterate. How can I reset this property?

Below is the sample code.

public interface Validate 
{
    public List validate();


    public class Options implements Serializable
    {
        public String name;
        public boolean enableProcessing = true;
        public Options(String name)
        {
            this.name = name;
        }
    }
}


public class Coder 
{
    public String name;
    public int age;
    public Coder(String name, int age)
    {
        this.name = name;
        this.age = age;
    }

    public void ValidateResult(Validate.Options option)
    {
        if(option.name.equals(this.name) && option.enableProcessing)
        {
            option.enableProcessing = false;
            //
            //business logic and function call
            //
        }
    }

    public static void main(String[] args) 
    {
        Validate.Options options = new Validate.Options("Test");
        List<Coder> coders = new ArrayList<Coder>();

        Coder coder = new Coder("Test", 28);
        Coder coder1 = new Coder("XYZ", 18);
        Coder coder2 = new Coder("Test", 16);       

        coders.add(coder);
        coders.add(coder1);
        coders.add(coder2);

        for(Coder co : coders)
        {
            co.ValidateResult(options);
        }
    }
}
4
  • you want enableProcessingto become true for coder1 ?? Commented Mar 30, 2016 at 6:28
  • 1
    Possible duplicate of Is Java "pass-by-reference" or "pass-by-value"? Commented Mar 30, 2016 at 6:28
  • You are passing the options to validateResult() and you are setting it there to false. What is the problem with ´options.enableProcessing = true´ ? Commented Mar 30, 2016 at 6:29
  • Please provide the code you have tried and that does not work. Commented Mar 30, 2016 at 6:32

2 Answers 2

2

If I understood the question well - in your for loop, simply add a line of code to reset the value of your public Validate.Options.enableProcessing field

for(Coder co : coders)
    {
        //reset options object for the next iteration
        options.enableProcessing = true;
        co.ValidateResult(options);
    }
Sign up to request clarification or add additional context in comments.

Comments

1

Make options immutable if you do not want it to be changed:

public class Options implements Serializable
{
    public final String name; // final prevents changes
    public final boolean enableProcessing = true; // final prevents changes
    public Options(String name)
    {
        this.name = name;
    }
}

To locally work with enableProcessing copy its value to a local variable.

public void ValidateResult(Validate.Options option)
{
    boolean enableProcessing = option.enableProcessing; // create local copy
    if(option.name.equals(this.name) && enableProcessing) // use local copy
    {
        enableProcessing = false; // only change local copy
        //
        //business logic and function call
        //
    }
}

Alternatively create new, fresh Options for each loop:

public static void main(String[] args) 
{
    List<Coder> coders = Arrays. asList(
        new Coder("Test", 28), 
        new Coder("XYZ", 18), 
        new Coder("Test", 16)
   );

   for(Coder co : coders)
   {
        Validate.Options options = new Validate.Options("Test"); // fresh options for each iteration
        co.ValidateResult(options);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.