The Class/Static methods are the methods that are called on the class itself, not on a specific object instance. The static modifier ensures implementation is the same across all the class instances. The class/static methods are called without instantiation means that static methods can only access other static members of the class. A few Java built-in static/class methods are Math.random(), System.gc(), Math.sqrt(), Math.random() and etc.
Syntax
public class className {
modifier static dataType methodName(inputParameters) {
// block of code to be executed
}
}Example
public class ClassMethodTest {
public static int findMinimum(int num1, int num2) {
int minimum = num2;
if (num1 < num2)
minimum = num1;
return minimum;
}
public static void main(String args[]) {
int min = ClassMethodTest.findMinimum(3, 5); // call this method without an instance.
System.out.println("ClassMethodTest.findMinimum(3, 5) is: " + min);
}
}Output
ClassMethodTest.findMinimum(3, 5) is : 3