Static methods are those methods that belong to a class, but they are not associated with any object of that class. That means, we can call a static method without creating any object of that class. Static methods are usually used for utility purposes, such as mathematical functions or other similar tasks. For example, the Math class in Java has several static methods that perform various mathematical operations, such as finding the square root of a number, or calculating the sine of an angle. These methods can be called without creating any objects of the Math class. Similarly, the System class in Java has several static utility methods that perform common tasks, such as copying an array or exiting the Java Virtual Machine.
To call a static method, we use the name of the class followed by the dot operator (.), followed by the name of the method. For example, to call the sqrt() method of the Math class, we would write:
Math.sqrt(4); // this will return 2
System.exit(0); // this will exit the program
If we try to call a static method on an object, we will get an error. For example, the following code will not compile:
Math m = new Math();
m.sqrt(4); // this will give an error
System.out.println(“This line will not be executed”);
However, we can call a static method from another static method of the same class. For example, the following code is valid:
public class Test {
public static void main(String[] args) {
Test.printMessage(); // this is valid
}
public static void printMessage() {
System.out.println(“Hello, world!”);
}
}
Static vs non static java
The main difference between static and non static in Java is that static is associated with class while non static is associated with objects. That means static can be used without creating an object while non static needs an object to be called. Static means something which exists independently of other things. For example, in mathematical terms, 1 is a static value because it exists by itself. On the other hand, 1+2 is not static because it doesn’t exist independently, it depends on 1 and 2. In Java programming language, we can apply the same analogy. A static variable or method exists independently of any instance of the class while a non-static variable or method is associated with objects.
Another difference between static and non static is that a static method can be called without creating an object while a non-static method cannot be called by just using the class name, we need an object to call it. In general, we don’t use static methods or variables as much because they break the flexibility of Object-Oriented Programming.
Static vs instance method in java
An instance method is a method that can only be called on an instance of a class (i.e. an object). On the other hand, a static method is a method that can be called without creating an instance of the class.
The main difference between static and instance methods in Java is that static methods belong to the class while instance methods belong to objects. That means we can call a static method without creating an object while we need an object to call an instance method. Another difference is that we can access static members of a class directly from the class name while we need an object to access non-static members.