i want to make a program that contains two main classes and they call each other.

class Main1
	{
	public static void main(String arg[])
		{
			for(int i=0;i<5;i++)
				System.out.println(i);
		Main2 m1=new Main2();
		}
	}
class Main2
	{
	public static void main(String arg[])
		{
			static int j=0;
			for(int i=0;i<5;i++)
				System.out.println(i);
			if(j==3)
			break;
			j++;
		Main1 m1=new Main2();
		}
	}

it is the code that i implemented. i use static variable so that i can control the no of calls but this shows an error when i use static variable.


please solve it for me..

And why would you want to have something like that????

permalink: coz i want to implement a dynamic structure program in java. for this i have to make this. so that i want to use this code.

To control the loop like that you just need an ordinary local variable within the method (which cannot, by definition, be static). If you need a single value that is shared between all instances of the class and all methods of the class then declare it static, but outside any method.

permalink: thanx buddy that was really very helpful.