• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Best practice for using main() method  RSS feed

 
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I would like some advice about how to use the main() method in my programs, and more specifically, what to put in there. Is it better to declare everything static and work like that or is it more preferable to create an instance of the main class and call another non static method and concider the new method as main() ? Also, consider this: I am given a university assignement where I have to come up with 2 algorithms that solve 2 different problems. I am asked to provide only one main method. Should I treat this main method as a merge of what would otherwise had been 2 main methods? For example for the first problem, check the arguments, then go ahead and call some methods of the one class and print the result and then continue to do something simillar for the second problem in the same main method. Or should I create 2 instances for the 2 other classes and call a non method that takes care of everything, for each of the 2 other classes?
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
coud you give some code example that you think
 
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Petros Papatheodoru wrote:Hello, I would like some advice about how to use the main() method in my programs, and more specifically, what to put in there. Is it better to declare everything static and work like that or is it more preferable to create an instance of the main class and call another non static method and concider the new method as main() ? Also, consider this: I am given a university assignement where I have to come up with 2 algorithms that solve 2 different problems. I am asked to provide only one main method. Should I treat this main method as a merge of what would otherwise had been 2 main methods? For example for the first problem, check the arguments, then go ahead and call some methods of the one class and print the result and then continue to do something simillar for the second problem in the same main method. Or should I create 2 instances for the 2 other classes and call a non method that takes care of everything, for each of the 2 other classes?

basically if you have main() method then creates a class file for every class which contains main() method, now if you want to do one main() method then it should be test class, then for operation you want perform individual task create class for every task and use polymorphic behavior  by  creating instance of each class and call them from test class by using reference of instance to which it is pointing,, hope it helps
 
Marshal
Posts: 64179
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Petros Papatheodoru wrote:Hello, I would like some advice about how to use the main() method in my programs, and more specifically, what to put in there.

Loook here, where you find the answer is, “as little as possible”.

Is it better to declare everything static . . .

No. Only declare something static if you have evidence that it needs to be static.

. . . 2 algorithms that solve 2 different problems. I am asked to provide only one main method. . . .

I would suggest you ask for clarification of what is required. It is strange to require an application carryng out two different tasks.
 
Petros Papatheodoru
Ranch Hand
Posts: 30
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have been really helpful guys. So, assuming I have to solve 2 exercises and use one main() the code should look like this:
 
Campbell Ritchie
Marshal
Posts: 64179
215
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but the class names should start E not e.
 
mooooooo ..... tiny ad ....
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!