Let's see what is the meaning of class, public, static, void, main, String[], System.out.println().
- class keyword is used to declare a class in java.
- public keyword is an access modifier which represents visibility, it means it is visible to all.
- static is a keyword, if we declare any method as static, it is known as static method. The core advantage of static method is that there is no need to create object to invoke the static method. The main method is executed by the JVM, so it doesn't require to create object to invoke the main method. So it saves memory.
- void is the return type of the method, it
means it doesn't return any value.
main represents startup of the program. - String[] args is used for command line argument. We will learn it later.
- System.out.println() is used print statement. We will learn about the internal working of System.out.println statement later.
package com.javasunderesan; public class MainMethodExample { public static void main(String[] args) { System.out.println("Welcome to http://javasunderesan.blogspot.in/"); } }
public static void main(string args) interview Questions
Why main method is static?
To execute main method without creating object then the main method oshould be static so that JVM will call main method by using class name itself.
-
Why main method return type is void?
main method wont return anything to JVM so its return type is void.
-
Why the name main?
This is the name which is configured in the JVM.
-
Why the arguments String[] args?
Command line arguments. String array variable name args : by the naming conventions they named like that we can write any valid variable name.
-
Can we write static public void main(String [] args)??
Yes we can define main method like static public void main(String[] args){}
Order of modifiers we can change.
-
What are the possible public static void main(String [] args)method declaration with String[] args?
- public static void main(String[] args){ }
- public static void main(String []args){ }
- public static void main(String args[] ){ }
- public static void main(String... args){ }
Instead of args can place valid java identifier.
-
Overloading main method
- We can declare multiple main methods with same name as per overloading concept.
- So we can overload main method but every time JVM looks for main method with String[] args method.
- Lets see an example program on main method overloading.
- main(String[] args) method will be called automatically by JVM.
- If we want to call other methods we need to call explicitly.
package com.javasunderesan; public class OverloadingMainMethod { public static void main(String[] args) { System.out.println("Java Sunderesan - Main method String [] args"); } public static void main(int[] args) { System.out.println("Java Sunderesan - Main method int[] args"); } }
No comments :
Post a Comment