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

What is a class/static method in Java?

王林
Release: 2023-08-18 18:20:56
forward
1103 people have browsed it

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
 }
}
Copy after login

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);
   }
}
Copy after login

Output

ClassMethodTest.findMinimum(3, 5) is : 3
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template