Kamis, 20 Maret 2014

Why main method is public static in Java




Main method in Java is the first programming method a Java programmer knows when he starts learning Java programming language.have you ever thought about why main method in Java is public, static and void, of-course Yes, since most of us first learn C and C++ than we move to Java in our programming path we familiar with main method but in Java main method is slightly different it doesn't return any value like in C it returns int, main method is public static and void Why? In this post we will try to find answer of these questions and have an idea of one of the most popular questions in Java why main method is declared Static.





What is main method in Java?



Main method in Java is entry point for any core Java program. Remember we are not talking about Servlet, MIDlet or any other container managed Java program where life cycle methods are provided to control the execution. In core Java program, execution starts from main method when you type java main-class-name, JVM search for public static void main(String args[]) method in that class and if it doesn't find that method it throws error NoSuchMethodError:main and terminates.








Signature of main method in Java





Main method has to strictly follow its syntax; other wise JVM will not be able to locate it and your program will not run. Here is the exact signature of main method





public static void main(String args[])





This signature is classic signature and there from start of Java but with introduction of  variable argument or varargs in Java5 you can also declare main method in Java using varargs syntax as shown in below example:





public static void main(String... args)





Remember varargs version of java main method will only work in Java 1.5 or later version. Apart from public, static and void there are certain keywords like final, synchronized and strictfp which are permitted in signature of java main method.





Why main method is static in Java



why main method is public static void in JavaNow come to the main point "Why main method is static in Java", there are quite a few reasons around but here are few reasons which make sense to me:





1. Since main method is static Java virtual Machine can call it without creating any instance of class which contains main method.





2. Since C and C++ also has similar main method which serves as entry point for program execution, following that convention will only help Java.





3. If main method were not declared static than JVM has to create instance of main Class and since constructor can be overloaded and can have arguments there would not be any certain and consistent way for JVM to find main method in Java.





4. Anything which is declared in class in Java comes under reference type and requires object to be created before using them but static method and static data are loaded into separate memory inside JVM called context which is created when a class is loaded. If main method is static than it will be loaded in JVM context and are available to execution.





Why main mehtod is public in Java


Java specifies several access modifiers e.g. private, protected and public. Any method or variable which is declared public in Java can be accessible from outside of that class. Since main method is public in


Java, JVM can easily access and execute it.





Why main method is void in Java


Since main method in Java is not supposed to return any value, its made void which simply means main is not returning anything.





Summary:


1. Main method must be declared public, static and void in Java otherwise JVM will not able to run Java program.





2. JVM throws NoSuchMethodException:main if it doesn't find main method of predefined signature in class which is provided to Java command. E.g. if you run java Helloworld than JVM will search for public static void main String args[]) method in HelloWorld.class file.





3. Main method is entry point for any Core Java program. Execution starts from main method.





4. Main method is run by a special thread called "main" thread in Java. Your Java program will be running until your main thread is running or any non-daemon thread spawned from main method is running.





5. When you see "Exception in Thread main” e.g.


Exception in Thread main: Java.lang.NullPointerException it means Exception is thrown inside main thread.





6. You can declare main method using varargs syntax from Java 1.5 onwards e.g.


public static void main(String... args)





7. Apart from static, void and public you can use final, synchronized and strictfp modifier in signature of main method in Java.





8. Main method in Java can be overloaded like any other method in Java but JVM will only call main method with specified signature specified above.





9. You can use throws clause in signature of main method and can throw any checked or unchecked Exception.





10. Static initializer block is executed even before JVM calls main method. They are executed when a Class is loaded into Memory by JVM.








Some tutorials you may like































Source:http://javarevisited.blogspot.com/2011/12/main-public-static-java-void-method-why.html

Tidak ada komentar:

Posting Komentar