Hi,
What if we declare the main method as private?
waiting for your responses!
Hi,
What if we declare the main method as private?
waiting for your responses!
The program compiles properly but at runtime it will give "Main method not public." message.
If you declare main() as private insted of public, we can compile the program but we cannot get the output.
main() is declared as public and static because
main() : public
it should be accessable by java interpreter .
if it is declared other than public it cannot be accessible by jvm because
it is only a packagelevel access.
main(): static
this is the method which calls other methods.
main() method is sharable by all methods & it is not particular to any object or class.
The static modifier means that it does not have to be instantiated to use it. Before a program runs there are technically no objects created yet, so the main method, which is the entry point for the application must be labeled static to tell the JVM that the method can be used without having first to create an instance of that class. Otherwise, it is the "Which came first, the chicken or the egg?" phenomenon. Your main method should be declared as follows:
public static void main (String[] args) { lots of your java code... }
hello ,iam Ekavir
as we know, java is a pure OOP , thats means every thing should be with in the class, main also,because main itself a function, static member functions should not refer through obj of that class. but,we can access static functions through class name itself.. like : class TestMain { public static void main(String args[]) { body; } } now, cmd>javac TestMain.java cmd>java TestMain as we know the static member functions has to call through its class name. thats why the program name must be same as the class name ,where we written the main function,ok!.
Bookmarks