• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Monique Johnson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you tried to compile it? (After fixing the numerous syntax errors)
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • 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: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot use the keyword private inside a method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!