• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I remove my main method from one class and access this method from my new class?  RSS feed

 
Papi Escobar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two seperate classes. Both classes contain a main method. Id like to remove the main method from my SaveData Class and accees the code from my DynamicTest Class. My main method is now located in DynamicTest class.



 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every class can have a main method. There is really nothing special about it - a class can even have several main methods if you follow overloading rules. You can call the various main methods like you'd call any other method.

The ONLY time main is special is when you start up java, and pass the JVM a class name. The JVM will then look for the one main method with the signature equivalent to



So if you want to run the main in your DynamicTest class, just pass that to the jvm:

>java DynamicTest

instead of

>java SaveData
 
Papi Escobar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the info fred rosenberger. Can you help me understand a couple other things im confused about?



I have a main method public static void main(String [] args)

in both classes. I want my main method in DynamicTest to run my program. Do I need to remove the main from SaveData? If so how do I properly do so and call that method from my DynamicTest class?

 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think this does what you think it does
read() is not guaranteed to read the full buffer's worth. Why would you want to break out of the loop if you aren't done reading the data?
 
Papi Escobar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not an issue with the program. If you could offer any insight regarding the original problem it would be greatly appreciated.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Papi Escobar wrote:Thanks for the info fred rosenberger. Can you help me understand a couple other things im confused about?
I have a main method public static void main(String [] args) in both classes. I want my main method in DynamicTest to run my program.

I think Fred was very clear on that.
Papi Escobar wrote:Do I need to remove the main from SaveData? If so how do I properly do so and call that method from my DynamicTest class?

You don't need to remove main() from SaveData. You can call

from you DynamicTest if you'd like.
 
Papi Escobar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
You don't need to remove main() from SaveData. You can call

from you DynamicTest if you'd like.


I would like to remove the main from SaveData. I dont want two main() methods in my program.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Papi Escobar wrote:
Carey Brown wrote:
You don't need to remove main() from SaveData. You can call

from you DynamicTest if you'd like.


I would like to remove the main from SaveData. I dont want two main() methods in my program.
So, what happens if you remove it? Is there some functionality you'll lose? Does it compile? Does it run?
 
Papi Escobar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote: . . . So, what happens if you remove it? Is there some functionality you'll lose? Does it compile? Does it run?


When I remove it the program doesnt run correctly. I needed to package SavedData and then import the SaveData class in DynamicTest, I then replaced main() with method and called this method from DynamicTest class.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Papi Escobar wrote:When I remove it the program doesnt run correctly. I needed to package SavedData and then import the SaveData class in DynamicTest, I then replaced main() with method and called this method from DynamicTest class.
Good work. So, am I to assume that your question has now been answered?
 
Papi Escobar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Good work. So, am I to assume that your question has now been answered?


Yes my issue has been resolved. Thanks to everyone for your feedback?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!