• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Is it correct to declare a class inside the main method ?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I saw an example where an (inner)class is declared inside the main method, please tell me if this is correct or not and why/when it's reasonable to use?

so smth like this

 
Sheriff
Posts: 67556
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happened when you tried to compile it? (After fixing the numerous syntax errors)
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monique Johnson wrote:I saw an example where an (inner)class is declared inside the main method, please tell me if this is correct or not and why/when it's reasonable to use?


Personally, I'd say not; and that has nothing to do with whether you can do it or not, but whether it's a good idea.

main() methods, in general, should be minimal. They should set up the class to be run, and then hand off execution to it.

For more information, have a look at the MainIsAPain page.

Winston
 
Marshal
Posts: 73284
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is one exception, where you are starting the Event Despatch Thread and require an anonymous Runnable class.
Where did you find that example? It looks particularly dreadful.
 
Campbell Ritchie
Marshal
Posts: 73284
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot use the keyword private inside a method.
reply
    Bookmark Topic Watch Topic
  • New Topic