Ads Sponsored By Google :)

Wednesday 23 January 2013

explain|means|meaning of public|static|void|main method|class in java can be (String args)|private|overidden|overriding|meant for


main method in java main in java public static void main in java meaning of public static void main string args in java explain main method in java explain main method java always static explain why main method java always static and public explain why main method java always static  main class in java example main class in java class public static void main(string args) public static void main meaning public static void main in java means main class in java can be private main class in java can be overridden main class in java can be overloaded public static void main in java explain public static void main in java public static void main in java meant for main class in java can main method be overloaded in java


Explanation Of main() statement :

      Public static void main(String args[]);
       Each and every java program starts executing from main() method. Hence main() method 
    is known as Program Driver.

       Number of statements of main method is set of executable statements which are internally 
    calling user defined methods. According to industry standards the block of statements return 
     in main() method is known as execution logic.

  Explanation

         1)      Main() method of java never returns any value, hence it return type must be Void.
         2)      Throughout the entire execution of java program, main() method will execute only once
            Hence the nature of main() method is Static.
        3)      Main() method can be accessible by every java programmer. Hence its access specifier  
                   is Public.
       4)      Each and every main() method of java takes array of objects String class (i.e String                
                 args[]).

  Can We overload main() in java or not ?

    In java we can overload the main() method, because JVM will look only for exact public static 
    void main(String[] args).

    For example:

   public class Demo {
    public static void main(String[] args) {
        System.out.println("First Main mehtod");
    }

    public static void main() {
        System.out.println("Second Main method");
    }

    public static void main(String arg1, String arg2) {
        System.out.println("third main method");
    }
    }

    Analysis:

   The above program will always prints First main method while we are running java Demo ... 
    from the command line, even if we specify more than one command-line argument.

    we can call the main() method yourself from code, of course - at which point the normal 
    overloading rules will be applied.

  Can Main method is overridden or Not?

       No, we cannot overridden the main method in java program. Because main method is 
    static (Public static void main(String args[]), static method cannot be oveeridden.

      For Example:
       Class demo1
     {
        Public static void main(String args[])
       {
        System.out.println(“this is overriding method”);
        }
     }
      Class demo2 extends demo1
     {
        Public static void main(String args[])
       {
         System.out.println(“this is overridden method”);
         }
      }
     Class maindemo
   {
     Public static void main(String args[])
    {
      //execution logic
     } 
   }


1 comment:

LinkWithin