Skip to main content

Main Method


Main Method
· Whether the class contains main() method or not and whether it is properly declared or
not these checkings are not responsibilities of the compiler, at runtime JVM is
responsible for this. If JVM unable to find the required main() method then we will get
runtime exception saying NoSuchMethodError: main.
Example:
          class Test
          {}
Output:
javac Test.java
java Test R.E: NoSuchMethodError: main
· JVM always searches for the main() method with the following signature.
· If we are performing any changes to the above signature then the code won’t run and
will get Runtime exception saying NoSuchMethodError. Anyway the following changes
are acceptable to the main() method.
1) The order of modifiers is not important that is instead of public static we can take static
public.
2) We can declare string[] in an acceptable form
           1) String[] args
           2) String []args
           3) String args[]
3) Instead of args, we can use any valid java identifier.
4) We can replace string[] with the var-arg parameter.
Example:
             main(String... args)
5) The main() method can be declared with the following modifiers.
· final, synchronized, Strictfp.
Which of the following main() method declarations are valid?
1) public static void main(String args){}(invalid)
2) public synchronized final Strictfp void main(String[] args){} (invalid)
3) public static void main(String... args){} (invalid)
4) public static int main(String[] args){}//int return type we can't take//(invalid)
5) public static synchronized final Strictfp void main(String... args){}(valid)
In which of the above cases we will get a compile-time error?
· No case, in all the cases we will get a runtime exception.
· Overloading of the main() method is possible but JVM always calls string[] argument
the main() method only.
Example:
    class Test
         {
               public static void main(String[] args)
                  {
                      System.out.println("String[] array main method");overloaded methods
                   }
              public static void main(int[] args)
                 {
                      System.out.Println("int[] array main method");
                  }
         }
Output:
String[] array main method
· The other overloaded method we have to call explicitly then only it will be executed.
· Inheritance concept is applicable for static methods including main() method hence
while executing child class if the child class doesn’t contain main() method then the
parent class main() method will be executed.
Example 1:
          class Parent
            {
              public static void main(String[] args)
                {
                     System.out.println("parent main"); Parent.java
                }
          }
        class Child extends Parent
           {}
Analysis:
Example 2:
        class Parent
           {
               public static void main(String[] args)
                    {
                          System.out.println("parent main");
                     }
           }
        class Child extends Parent Parent.java
            {
                 public static void main(String[] args)
                       {
                            System.out.Println("Child main");
                       }
            }
Analysis:
· It seems to be overriding concept is applicable for static methods but it is not overriding
it is method hiding.
Command-line arguments:
· The arguments which are passing from command prompt are called command-line
arguments. The main objective of command-line arguments is we can customize the
behavior of the main() method.
Example 1:
   class Test
         {
              public static void main(String[] args)
                   {
                        for(int i=0;i<=args.length;i++)
                            {
                                    System.out.println(args[i]);
                             }
                    }
        }
Output:
java Test x y z
ArrayIndexOutOfBoundsException: 3
Example 2:
· Replace i<=args.length with i<args.length then it will run successfully.
· Within the main() method command line arguments are available in the form of String
hence “+” operator acts as string concatenation but not arithmetic addition.
Example:
       class Test
           {
                 public static void main(String[] args)
                     {
                          System.out.println(args[0]+args[1]);
                     }
            }
Output:
E:\SCJP>javac Test.java
E:\SCJP>java Test 10 20
1020
· Space is the separator between 2 command line arguments and if our command line
argument itself contains space then we should enclose with in double quotes.
Example:
        class Test
           {
               public static void main(String[] args)
                  {
                      System.out.println(args[0]);
                  }
          }
Output:
E:\SCJP>javac Test.java
E:\SCJP>java Test "Navneet singh"
Navneet singh

Comments

Post a Comment

Popular posts from this blog

Core Java

Content 1 - Language fundamental   Identifier Reserved words Data types Literals Arrays Types of variables Var arg method Main method Java coding standards 2 -  Declaration and Access Modifiers Java source file structure Class modifiers Member modifiers Interfaces 3 - Exception Handling Introduction Runtime stack mechanism Default exception handling in java Exception hierarchy Customized exception handling by try-catch  Control flow in try-catch Methods to print exception information Try with multiple catch blocks Finally Difference between final, finally, finalize Control flow in try-catch-finally Control flow in nested try-catch-finally Various possible combinations of try-catch-finally throw keyword throws keyword Exception handling keywords summary Various possible compile-time errors in exception handling Customized exceptions Top-10 exceptions  4 - String Concept of String Immutable String String Compa...