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);
        }
    }
}
  • you want `enableProcessing`to become true for coder1 ?? –  Mar 30 '16 at 06:28
  • 1
    Possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Yassin Hajaj Mar 30 '16 at 06:28
  • You are passing the options to validateResult() and you are setting it there to false. What is the problem with ´options.enableProcessing = true´ ? – tak3shi Mar 30 '16 at 06:29
  • Please provide the code you have tried and that does not work. – tak3shi Mar 30 '16 at 06:32

2 Answers2

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);
    }
Jas
  • 1,141
  • 5
  • 16
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);
    }
}
slartidan
  • 20,403
  • 15
  • 83
  • 131