This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Main methods in 2 separate classes

 
Charu Singh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm caught in a situation where I need to access a variable (say i)from a class (say A) from another class B.

class A{
public static int i;
public static void main(String args[]){
i = i+20;
while(true){
Thread.sleep(1000);
}
}

class B {
public static void main(String args[]){
System.out.pintln("Incremented value of i = "+A.i);

}
}
Now, this code always prints 0, what can I do to make the code print he incremented value? I cannot put the i++ statement in a separate method as I do not have write access to the class A.

Thanks for your suggestions.

}
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are several comments one could make about your code. First of all, two mains typically implies two applications. Perhaps the best way to procede is to ask: what are you trying to do, what is your goal? When you aswer that, forum members can give you more specific advice.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
B can invoke a method on A to make things happen. main() is a regular method, so it might look like:

But your current design is not going to work. The A main loops forever, so the call to A.main will never return. Take the loop out of A and see if that change to B.main() helps. Give it another shot and show us how it works!

If you get the mechanics of the call working, we can talk about object design. We probably wouldn't use all static methods and variables, and we'd probably make another method besides main() to do the increment.
[ March 27, 2006: Message edited by: Stan James ]
 
Charu Singh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,

A little more insight into the actual situation. I have 2 classes in an application that was written long back. We are maintaining and enhancing the code. Just like class A here, one of my classes has a 1000 line code in main method; it prints to various logs and performs different operations. Now what I need is the value of a variable after all the opertaions have been performed. If I move it to another method and call it from the main method of class B, all the opertaion (1000 line code)will be executed twice(once when I call it from the main method of A and once from the main method of B) . Now, isn't that a lot of overwork for the application.

I'm not sure what approach to follow; first time facing a situation like that. It will be great if someone can suggest.

Thanks a ton.
[ March 27, 2006: Message edited by: Charu Singh ]
 
Ashish Chopra
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your case, it is clear that class B depends on class As main to be executed so that class B can go ahead. The best way to do this is to restructure A as follows:


Now when A is running standalone, it will do its job. And when B will need the result of A's execution, it needs to call the "thousandLineMethod()" and simply check the return value.

Of course this solution is based on many assumptions, one of which is that these are not interlinked classes. And if they are, there should be only one main method, as it is always advisable to have a single entry point in your applications.

Hope this helps.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Charu Singh:
one of my classes has a 1000 line code in main method;


*Shudder* Are you sure you want to maintain that?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic