A method is a block of code which only runs when it is called.
You can pass data, known as parameters, into a method.
Methods are used to perform certain actions, and they are also known as functions.
Why use methods? To reuse code: define the code once, and use it many times.
Create a Method
A method must be declared within a class. It is defined with the name of the method, followed by
parentheses []. Java provides some pre-defined methods, such as System.out.println[]
, but you can also create your own methods to perform certain actions:
Example
Create a method inside Main:
public class Main {
static void myMethod[] {
// code to be executed
}
}
Example Explained
myMethod[]
is the name of the methodstatic
means that the method belongs to the Main class and not an object of the Main class. You will learn more about objects and how to access methods through objects later in this tutorial.void
means that this method does not have a return value. You will learn more about return values later in this chapter
Call a Method
To call a method in Java, write the method's name followed by two parentheses [] and a semicolon;
In the following example, myMethod[]
is used to print a text [the action], when it is called:
Example
Inside main
, call the
myMethod[]
method:
public class Main {
static void myMethod[] {
System.out.println["I just got executed!"];
}
public static void main[String[] args] {
myMethod[];
}
}
// Outputs "I just got executed!"
Try it Yourself »
A method can also be called multiple times:
Example
public class Main {
static void myMethod[] {
System.out.println["I just got executed!"];
}
public static void main[String[] args] {
myMethod[];
myMethod[];
myMethod[];
}
}
// I just got executed!
// I just got executed!
// I just got executed!
Try it Yourself »
In the next chapter, Method Parameters, you will learn how to pass data [parameters] into a method.
Test Yourself With Exercises
Exercise:
Insert the missing part to call myMethod
from main
.
static void myMethod[] { System.out.println["I just got executed!"]; } public static void main[String[] args] { ; }
Start the Exercise
Following points explain what is “static” in the main[] method:
- main[] method: The main[] method, in Java, is the entry point for the JVM[Java Virtual Machine] into the java program. JVM launches the java program by invoking the main[] method.
- Static is a keyword. The role of adding static before any entity is to make that entity a class entity. It means that adding static before methods and variables make them class methods and class variables respectively, instead of instance methods and instance variables.
Hence, static methods and variables can be directly accessed with the help of Class, which means that there is no need to create objects in order to access static methods or variables.
// Making a function as static
static void func[]
{}
// Making a variable as static
static int var;
- Static methods: When a method is declared with static keyword, it is known as static method. As discussed above, any static member can be accessed before any objects of its class are created, and without reference to any object.
// Making a static function
class Prutor
{
static void func[]
{}
}
// Calling a static function
Prutor.func[];
- Static main[] method: When the static keyword is added in the function definition of main[] method, then it is known as static main[] method.
class Prutor
{
// Making a static main function
public static void main[String[] args]
{}
}
- Need of static in main[] method: Since main[] method is the entry point of any Java application, hence making the main[] method as static is mandatory due to following reasons:
- The static main[] method makes it very clear for the JVM to call it for launching the Java Application. Otherwise, it would be required to specify the entry function for each Java application build, for the JVM to launch the application.
- The method is static because otherwise
there would be ambiguity which constructor should be called.
Example, if the class looks like this:
public class Prutor{
protected Prutor[int g]{}
public void main[String[] args]{
}
}
- The JVM now enters an ambiguity state deciding whether it should call new Prutor[int]? If yes, then what should it pass for g? If not, then should the JVM instantiate Prutor without executing any constructor method?
There are just too many edge cases and ambiguities like this for it to make sense for the JVM to have to instantiate a class before the entry point is called. That’s why main is static. - The main[] method is static because its convenient for the JDK. Consider a scenario where it’s not mandatory to make main[] method static. Then in this case, that just makes it harder on various IDEs to auto-detect the ‘launchable’ classes in a project. Hence making it a convention to make the entry method ‘main[]’ as ‘public static void main[String[] args]’ is convenient.
What if we don’t write “static” before the main method: If we do not write “static” before the main method then, our program will be compiled without any compilation error[s]. But at the time of execution, the JVM searches for the main method which is public, static, with a return type and a String array as an argument. If such a method is not found then an error is generated at the run time.
/*package whatever //do not write package name here */
import java.io.*;
class Prutor {
public void main [String[] args] {
System.out.println["Prutor!"];
}
}
Output:
Error: Main method not found in class,please define main method as:
public static void main[String[] args]