aspose file tools*
The moose likes Beginning Java and the fly likes Main methods in 2 separate classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Main methods in 2 separate classes" Watch "Main methods in 2 separate classes" New topic
Author

Main methods in 2 separate classes

Charu Singh
Greenhorn

Joined: Jun 10, 2005
Posts: 20
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

Joined: Sep 16, 2005
Posts: 1780
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.


There is no emoticon for what I am feeling!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
Charu Singh
Greenhorn

Joined: Jun 10, 2005
Posts: 20
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

Joined: Nov 30, 2004
Posts: 134
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.


Quis Custodiet Ipsos Custodes<br /> <br />My blog: <a href="http://www.coherentrambling.blogspot.com" target="_blank" rel="nofollow">http://www.coherentrambling.blogspot.com</a>
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Main methods in 2 separate classes