954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help

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..

hackit
Newbie Poster
18 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

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

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

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.

hackit
Newbie Poster
18 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

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.

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

permalink: thanx buddy that was really very helpful.

hackit
Newbie Poster
18 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: