• 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

static inner class  RSS feed

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could someone tell me why the following doesnt run? I thought inner static class can contain main().
public class a {
Could someone tell me why the following doesnt run? I thought inner static class can contain main().
public class c {
static int i;
static class d{
public static void main(String args[]){
System.out.println("blah");
System.out.println(i);
}
}}
static class b{
public static void main(String args[]){
System.out.println("blah");
}
}}
 
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will work which you wrote , the reason why is because you only allowed to have inner static classes . you are howerver not allowed to have top static classes

!! not allowed

This is not allowed
 
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only members of interfaces and top level classes can be static. So NO you can not put the main method in an inner class.
In addition main needs to be in the class that matches the name of the file that contains it.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!