user2331283
user2331283

Reputation:

Java programming error Pass by reference

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);
        }
    }
}

Upvotes: 1

Views: 75

Answers (2)

slartidan
slartidan

Reputation: 21566

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);
    }
}

Upvotes: 1

Jas
Jas

Reputation: 1141

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);
    }

Upvotes: 2

Related Questions