• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Bartender
Posts: 10979
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 10979
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 10979
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 10979
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Grow a forest with seedballs and this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic