Java- What is the main( ) method in JAVA / Chapter - 7 /Language Fundamental


java main

java main method


You must be thinking that a particular line which always there in every java program is:-                            
public static void main(String [ ] args)

Q.what is this ???
A.The answer is this the main( ) method of java. The starting point from where the JVM starts reading &
     executing the java program.

  1. Whether a class contains main( ) method or not and whether main( ) method declared according to requirements or not these things won't checked by Compiler.

  2. At Runtime JVM is responsible for checking all these things.

  3. If JVM is unable to find main( ) method then we will get Runtime Exception saying"no such method error: main" (1.6 v).In 

  4. In 1.7 v of java, we will get "Error: main method not found in class Test, please define the main method as - public static void main(String [ ] args)".


example 1:-

           class Test

          {

         

          }

                  javac Test.java    (It compiles fine 1.6 v)

                  java Test   (RuntimeException:NoSuchMethodError: main)


example 2:-

           class Test

          {

         

          }

              
    javac Test.java    (It compiles fine 1.7 v)

  java Test   ("Error: main method not found in class Test ,please define the main method as:-       


                  public static void main(String [ ] args)"


             


Now let's understand  the syntax of the main( ) method 


                      Syntax:-

                                  public static void main(String [ ] args)

 


  • public: -  This method has to call by JVM from anywhere.Means JVM can be locating anywhere like local disk C: local disk D:, local disk E: or anywhere else has to call the main ( ) method that is why it is public.

  • static: -Without existing object also JVM has to call this method. Because static block or method load at the time of class loading hence to execute main( ) no need of object reference to call this method.

  • void: - The main( ) method won't return anything to java that's why it is declared as void.

  • main: - This the name is being assigned or configured in java by java makers.For identification purpose.

  • String [ ] args: -This is command line argument.


 5.The above syntax is fixed and very strict. If we perform any change then we will get RuntimeException saying"no such method error: main" (until 1.6 v). And from 1.7v onwards we will get "Error: main method not found in class Test, please define the main method as- public static void main(String [ ] args)".

6.Even though the Syntax is very strict we can change the order of modifiers.


example:-  

                 public static void main(String[ ]  args)          (valid)


                 static public void main(String[ ]  args)          (valid)

                  void static public main(String[ ]  args)         (valid)

All above three declarations are all valid. 

 

 

7. We can declare String  [ ] in the following form also.

example:- 

         main(String[ ]    args)                                       (valid)

         main(String      [ ] args)                                       (valid)

         main(String args[ ])                                              (valid)

All above three declarations are all valid.

 

 

8. We can replace String [ ] with String... var-arg parameters.

example : 

              public static void main(String...    args)              (valid)

 

 

9. We can declare the main method with three more modifiers.


  • final

  • synchronized

  • strictfp


example:-       


                public static final synchronized strictfp void main(String[ ]  args)        (valid)


Amazed to see that. Yes, the above syntax of the main( ) method is absolutely valid.Don't believe then try it.

 

 


Now check your skills.


Q. which of the following main method declarations are valid.


                 public static void main(String  args)

                 public static void Main(String [ ] args)

                 public void main(String [ ] args)

                 public static void main(String [ ] args)

                 public static int main(String [ ] args)

                 public static void main(String [ ] args)

                 public final synchronized strictfp void main(String[ ]  args)

                 public static final synchronized strictfp void main(String[ ]  args)

                 public static void main(String...   args)

                 public static void main(String[ ]   args)

 

 

 

 

 

 

           Now check your answers:-


             public static void main(String  args)                 (invalid)

             public static void Main(String [ ] args)               (invalid)

             public void main(String [ ] args)                           (invalid)

             public static void main(String [ ] args)                  (invalid)

             public static int main(String [ ] args)                   (invalid)

             public static void main(String [ ] args)                   (invalid)

             public final synchronized strictfp void main(String[ ]  args)           (invalid)

             public static final synchronized strictfp void main(String[ ]  args)     (valid)

             public static void main(String...   args)        (valid)

             public static void main(String[ ]   arg          (valid) 


No comments:

Post a Comment

Be the first to comment!

Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.

Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.

Regards,
JavaByChetan
Back To Home

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com