-3

Is there any way to update the loop variable in for loop from another class in java?

For example let's consider a class mytest.java as follows:

 public class mytest()
        {
                public static void main(String[] args)
                {
                   for(int i=0;i<10;i++)
                   {
                       new checker().check(i);
                   }
                }
        }

Now consider class checker.java as follows:

public class checker()
{
    public boolean check(int i)
    {
         if(i==5)
         {
             //Here if value of i is 5, i don't want to do any 
             //more operation but just update the for loop of
             // of mytest class and continue with later iterations.
         }
         else
         {
              //there may be many more operations like calling other
              // methods or other class.
         }
   }
}
  • No, but you *can* use a field, which is accessible from outside the current method. – nanofarad Jun 19 '15 at 12:03
  • Have a look to understand why : http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – Laurent B Jun 19 '15 at 12:18
  • So you mean that is not possible? – dividesByZero Jun 19 '15 at 12:23
  • What's wrong with `if (i != 5) { ... }` in your class `checker` (by the way: please name it `Checker`)? If the loop variable is 5, the checker does nothing and returns. The loop variable is then automatically incremented. – Seelenvirtuose Jun 19 '15 at 12:35

2 Answers2

0

You can return a value from check() method as integer and assign it to for loop variable i.

As per your example:

public class checker
{
    public int check(int i)
    {
         if(i==5)
         {
             //Here if value of i is 5, i don't want to do any 
             //more operation but just update the for loop of
             // of mytest class and continue with later iterations.
         }
         else
         {
              //there may be many more operations like calling other
              // methods or other class.
         }
       return i;

   }
}

And in your mytest class you can assign the value to for loop variable.

public class mytest
{
      public static void main(String[] args)
      {
            for(int i=0;i<10;i++)
            {
                int temp = new checker().check(i);
                if (condition)
                  i= temp;
             }
        }
 }
Abhijeet Dhumal
  • 1,799
  • 13
  • 24
0

You cannot assign the loop variable from another class and I'm glad you can't do this! Now this is not completely what you requested and not the prettiest code out there, but it'll do the job:

public class myTest {

private int loopVariable;


public static void main(String[] args)
{
    myTest myTest = new myTest();
    myTest.mainLoopd(myTest);
}

public void mainLoopd(myTest myTest){
    checker checker = new checker(myTest);

    for(loopVariable=0;loopVariable<10;loopVariable++)
    {
        checker.check();
        System.out.println(loopVariable);
    }
}

public int getLoopVariable(){
    return this.loopVariable;
}

public void setLoopVariable(int loopVariable){
    this.loopVariable = loopVariable;
}

}

And the checker class will look like:

public class checker {

private myTest myTest;

public checker(myTest myTest){
    this.myTest = myTest;
}

public void check()
{
    if(this.myTest.getLoopVariable()==2)
    {
        this.myTest.setLoopVariable(5);
    }
    else
    {
        //there may be many more operations like calling other
        // methods or other class.
    }
}

}

Hope it helps

  • I think you didn't understand the essence of my problem. I don't just want to update the loop variable but the next iteration should be executed as soon as if statement returns true in checker class. What your code does is set loop variable but continue with other parts. Remember check method can have many operation after if statement which should be executed if if statement doesn't true. – dividesByZero Jun 22 '15 at 07:14