Home >Java >javaTutorial >What is a class/static method in Java?

What is a class/static method in Java?

王林
王林forward
2023-08-18 18:20:561126browse

What is a class/static method in Java?

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

Grammar

public class className {
 modifier static dataType methodName(inputParameters) {
    // block of code to be executed
 }
}

Example

is:

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); // <strong>call this method without an instance.</strong>
      System.out.println("ClassMethodTest.findMinimum(3, 5) is: " + min);
   }
}

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete