In this article we learn the what is method in java, what is main method in java and what is the difference between static and non-static method in java.
What is Method in Java?
1. A method in java is just a block or collection of statements that perform some specific tasks. To perform the task, we call the method so that compiler executes the statements written in the method.
2. Main method in java is always static.
3. Syntax:
accessSpecifier returnType methodName (para/Args list) {
// Method body
}
Access Specifier: The Access specifier is also known as Access modifier in java that specifies the visibility of the method.
Return Type: The return type tells which type of data value a method will return. A method can return a type of value like primitive or non-primitive. But if you don’t want to return any value then use the void keyword.
Method Name: It is a unique name that is used to define the name of a method.
Parameter /Args list: The parameter list/ Argument list is used when we want to send the data in method and want to perform some action on it. We can send any number of parameter lists separated by a comma and enclosed in the pair of parentheses. But here we tell the data type and name of each parameter. We can leave it as blank as it is not mandatory.
Method Body: The body of the Java method should be enclosed within curly braces {}. All the code for a method is written in its body within braces. All the operations and tasks are performed inside a Java method.
Explain public static void main (String [] args) in Java | What is main method in Java?
This statement is declaring a main () method of a Java class. Let’s discuss each of its keywords.
Public: This is one of the access modifiers that means that the method is accessible anywhere by any class.
Static: static keyword tells that we can access the main () method without creating the object of the class.
void: The void keyword tells that the main () method returns nothing.
main: This is the name of the method.
String args []: args [] is the name of the String array. It contains command line arguments in it that the users can pass while executing the program.
What is the difference between static and non-static method in Java?
1. Non-static methods are the normal method that can access any static variables and static methods.
2. Static methods are declared with a static keyword and can only access static data members of the main class or another class but are unable to access non- static methods and variables.
3. The second difference is that we can call a static method without creating an object of the class, but we cannot call non-static members directly through class, we can only call by creating an object of the class.
Is it possible to have multiple main methods in the same class?
No, we cannot multiple main () methods in the same class. If we do so, the program fails to compile. The compiler gives the error saying that the main method is already defined in the class.
Why main method () always declared with static?
1. Main method is always static because non-static members or methods should not be called with the class name directly. static keyword tells that we can access the main () method without creating the object of the class.
2. The main method is static in Java, so the JVM can directly invoke it without instantiating the class’s object.
3. If the main method is non-static, then JVM needs to create an instance of the class, and there would be ambiguity if the constructor of that class takes an argument – which constructor should be called by JVM and what parameters should be passed? We know that JVM can’t instantiate a Java class without calling a constructor method. We can agree upon a default constructor, but that’s extra overhead. Also, the class must not be abstract; otherwise, the JVM could not instantiate it. To make Java a little less verbose than it already is, the main method is static in Java
Why main method public in Java?
We know that anyone can access/invoke a method having public access specifier. The main method is public in Java because it has to be invoked by the JVM. So, if main () is not public in Java, the JVM won’t call it.
Other Popular Articles
Java Memory Management | Difference Between JAR and WAR Files in Java