What if main method is inside “non public class” of java file?

后端 未结 7 2141
半阙折子戏
半阙折子戏 2020-11-30 12:40

I have a java file containing more than one class, out of which one is public. If main method is inside a non-public class. I can\'t run that java file. Why is that? and the

相关标签:
7条回答
  • 2020-11-30 13:02

    You can certainly override main method and it does not violate any compiler rules and hence you will not have any compiler errors.

    You check that inspite of the fact that you have more than one class a file that is declared as public is the name of the file you are trying to execute.

    This is a convention that the file should be named after the same class which is public in that code.

    Hence when you try to execute that class it does not have a main method from which it starts execution.So if you want to execute the main method in the non public class the only way to this is call that main from a main method of the public class.

    0 讨论(0)
  • 2020-11-30 13:02

    Actually you can execute the main method in a non-public class. if you put this class

    class A {
      public static void main(String... args) {
          System.out.println("This is not a public class!");
      }
    }
    

    in a file named NonPubClass.java. You can compile this file using javac command but you will not get a NonPubClass.class, you will get a A.class instead. Use java a to invoke that class and you will see the printed string --- This is not a public class!

    0 讨论(0)
  • 2020-11-30 13:12

    there is something i would like to add although everybody here believes that a public is necessary for the main in a class and that it won't work without main

    you can have as many mains in a class as you desire, and you can have them without a public access modifier. but be careful, only that class which is named after the file can be public what i mean is if you name your file a.java , then only the class with name a can be public, none other can have this facility

    here is a code to show this : as you can see the name of the file is helping.java

    //:initialization/helping.java
    
    class b{
        public static void main(){
            System.out.println("hello its b");
        }
    }   
    
    class helping {
        static void f(float i, Character... c) {
            System.out.println("first");
        }
        static void f(char a, Character... args) {
            System.out.println("second");
        }
        public static void main(String[] args) {
            f(1,'a');
            f('a','b');
            c.main();
        }
    }
    
    class c{
        public static void main(){
            System.out.println("hello its b");
        }
    }
    //:~
    /*
     * output:  
     * first
     * second
     * hello its b  
     * */
    
    0 讨论(0)
  • 2020-11-30 13:13

    Have a look at this code:
    Super.java

    public class Super{ }
    class Sub{
        public static void main(String[] s){
            System.out.println("Hello");
        }
    }
    

    In order to print Hello you can compile and run the program as:

    console output

    How this works?
    The compiler generates separate .class file for every class in your program. So, instead of calling the main() of non-public class from the public class's main() you can print the output as shown above.

    Note: As the convention says, you must put a public class in separate file <class_name>.java. And do not put more than one class in a single file (except if they are inner class) because if you would like to import them or use them with other classes then it will cause problem.

    0 讨论(0)
  • 2020-11-30 13:18

    Simple Answer. You can't. You need to have main method in a public class and its signature should be public static void main(String... args)

    there is no compilation error

    Why there would be? You are doing nothing wrong as far as compilation rules are concerned. Only thing is that your non-public-class-main-method won't work as an entry point of your code.

    0 讨论(0)
  • 2020-11-30 13:23

    It is a compile-time error if a top level type declaration contains any one of the following access modifiers: protected, private, or static.This link may be helpful.

    0 讨论(0)
提交回复
热议问题