Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!