How to call a Java static method
In Java, static methods can be called using either of the following two ways:
- To call a static method, use the class name followed by a dot and the static method name. For example, if the static method name is printMessage(), you can call it using ClassName.printMessage(). ClassName is the name of the class that contains the static method.
- To call a static method using the object name, use the format objectName.staticMethodName(). While it is not recommended to call static methods using the object name, the compiler will automatically convert it to use the class name instead. For example, if the static method is named printMessage(), it can be called using objectName.printMessage(). Here, objectName is a reference to the object containing the static method.
It is worth noting that static methods can only call other static methods and access static variables, not call non-static methods or access non-static variables.