Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

java coding style - without main()  RSS feed

 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I seen many of the codes does not have main method and it has initialization and object creation everything within the constructor itself.
What are the benefits of this coding style? Any performance improvement?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's nothing wrong with a class like that. But the problem is that you can't execute an application in Java with the main method.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Real Java applications often consist of hundreds if not thousands of classes; only one of those needs to have a main() routine. The main() is generally used just to create a few objects, hook them up, and start some threads.

Of course, these days, there are far more "Web applications" than desktop applications; in these, the programmer doesn't write any main() at all. The main() routine is inside the application server, and the "webapp" programmer simply write classes that implement specific interfaces to create an application.

The only time you'll generally see an entire application written as one class is in a student program; programs in the real world are virtually never like that.
 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Keith, What is the differnce between the calling the methods within main thru its object and without main, within the constructor itself directly calling the methods?
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
main is nothing more or less than the function the virtual machine calls to start your application.
It's no different from any other function except for that.

You seem to be programming everything as a huge block of code in your main method. It's about time you started getting some good Java books and learning to use the language like it's intended and not as a substitute for GWBASIC.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!