• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java coding style - without main()

 
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic