Why can't we define main function in static inner classes?

Aniket Thakur

I have the following simple code

public class Tester {
    static class TesterChild {
        public static void main(String args[]) {
            System.out.println("Test");
        }
    }
}

It compiles fine. But when I run it I get the following Error

[aniket@localhost src]$ java Tester
Error: Could not find or load main class Tester

Question is why can't we define our main method in static inner class?

Update1 :

As specified in the answers/comments I have change the code to following

public class Tester {
    public static class TesterChild {
        public static void main(String args[]) {
            System.out.println("Test");
        }
    }
}

I compiled it and it made two class files Tester.class and Tester$TesterChild.class. But still i am getting error

[aniket@localhost Desktop]$ java Tester$TesterChild
Error: Could not find or load main class Test

Update 2:

Ok now I included current directory in the classpath and executed still getting error

[aniket@localhost Desktop]$ java -cp . Tester$TesterChild
Error: Main method not found in class Tester, please define the main method as:
   public static void main(String[] args
Adam Arold

It can be run as main but you are not using the right class. Your main class is not Tester but Tester.TesterChild.

In Eclipse it will run without any setup but from the command line you have to use the java 'yourpackage.Tester$TesterChild' syntax as others mentioned above.

You need to wrap the name of your class in ''s because on linux/unix the shell might think that $TesterChild is a variable. If you try it out in the prompt you will get something like this if you omit the ''s:

Error: Could not find or load main class Tester

If you need to explicitly set the classpath you can use the -cp or the -classpath option or you can set it from the commandline: set CLASSPATH=/somedir

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why can't we define function inside the main function?

From Dev

Top level classes cannot be static in java but inner classes can be why ?

From Dev

Top level classes cannot be static in java but inner classes can be why ?

From Dev

Can we define structs after the main function in C++?

From Dev

Why is it not possible to define a static (public) member of a class in the main() function?

From Dev

Why can't outer classes extend inner classes?

From Dev

why can't we define array in structures in C#

From Dev

Why can't we define struct pointers to null in the beginning

From Dev

Why can't we define a variable just after if statement follows

From Dev

why can't we define array in structures in C#

From Dev

Why can't I declare and initialize static variable in inner class?

From Dev

why can't we add elements to ArrayList outside of the main() method?

From Dev

why can't we add elements to ArrayList outside of the main() method?

From Dev

Why we use parameters in function when we can define variables in body of function?

From Dev

Why can't we add static methods to enums?

From Dev

Why can't we use @Cacheable with static method in spring with ehcache?

From Dev

Why can't we use auto in template static member initialization?

From Dev

Why can't we use this instance in the static method of extended class?

From Dev

Why can't we switch on classes in Java 7+?

From Dev

Can we define static functions in tclOO?

From Dev

Can we define static functions in tclOO?

From Java

Why can't I define a static method in a Java interface?

From Dev

why we can't initialize static variable in constructor initialization list , but we can in constructor body

From Dev

why we can't initialize static variable in constructor initialization list , but we can in constructor body

From Dev

Why local classes in Java can`t have static methods?

From Dev

Why local classes in Java can`t have static methods?

From Java

Why can't I define a function inside another function?

From Dev

Why can't a typedef of a function be used to define a function?

From Dev

Why can’t this static inner class call a non-static method on its outer class?

Related Related

  1. 1

    Why can't we define function inside the main function?

  2. 2

    Top level classes cannot be static in java but inner classes can be why ?

  3. 3

    Top level classes cannot be static in java but inner classes can be why ?

  4. 4

    Can we define structs after the main function in C++?

  5. 5

    Why is it not possible to define a static (public) member of a class in the main() function?

  6. 6

    Why can't outer classes extend inner classes?

  7. 7

    why can't we define array in structures in C#

  8. 8

    Why can't we define struct pointers to null in the beginning

  9. 9

    Why can't we define a variable just after if statement follows

  10. 10

    why can't we define array in structures in C#

  11. 11

    Why can't I declare and initialize static variable in inner class?

  12. 12

    why can't we add elements to ArrayList outside of the main() method?

  13. 13

    why can't we add elements to ArrayList outside of the main() method?

  14. 14

    Why we use parameters in function when we can define variables in body of function?

  15. 15

    Why can't we add static methods to enums?

  16. 16

    Why can't we use @Cacheable with static method in spring with ehcache?

  17. 17

    Why can't we use auto in template static member initialization?

  18. 18

    Why can't we use this instance in the static method of extended class?

  19. 19

    Why can't we switch on classes in Java 7+?

  20. 20

    Can we define static functions in tclOO?

  21. 21

    Can we define static functions in tclOO?

  22. 22

    Why can't I define a static method in a Java interface?

  23. 23

    why we can't initialize static variable in constructor initialization list , but we can in constructor body

  24. 24

    why we can't initialize static variable in constructor initialization list , but we can in constructor body

  25. 25

    Why local classes in Java can`t have static methods?

  26. 26

    Why local classes in Java can`t have static methods?

  27. 27

    Why can't I define a function inside another function?

  28. 28

    Why can't a typedef of a function be used to define a function?

  29. 29

    Why can’t this static inner class call a non-static method on its outer class?

HotTag

Archive