This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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 ]
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Jun 10, 2005
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 ]
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.