Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

java coding style - without main()

 
Ranch Hand
Posts: 1490
  • 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?
 
Ranch Hand
Posts: 2410
  • 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.
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • 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: 1490
  • 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?
 
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!