Static methods are methods that are called on the class itself, rather than on a specific object instance. The static modifier ensures that the implementation is the same across all class instances. Class/static methods are callable without instantiation , which means static methods can only access other static members of the class. Some of Java's built-in static/class methods include Math.random(), System.gc(), Math.sqrt(), Math.random(), etc. The Chinese translation of
public class className { modifier static dataType methodName(inputParameters) { // block of code to be executed } }
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); // <strong>call this method without an instance.</strong> System.out.println("ClassMethodTest.findMinimum(3, 5) is: " + min); } }
ClassMethodTest.findMinimum(3, 5) is : 3
The above is the detailed content of What is a class/static method in Java?. For more information, please follow other related articles on the PHP Chinese website!