class Languages {
public static void main(String[] args) {
display();
}
static void display() {
System.out.println("Java is my favorite programming language.");
}
}
Java static method vs instance method
Calling an instance method requires the creation of an object of its class, while a static method doesn't require it.
public static void main(String[] args) {
display(); //calling without object
Difference t = new Difference();
t.show(); //calling using object
}
static void display() {
System.out.println("Programming is amazing.");
}
void show(){
System.out.println("Java is awesome.");
}
}
How to call a static method in Java?
If you wish to call a static method of another class, then you have to mention the class name while calling it as shown in the example:
class Another {
public static void main(String[] args) {
int result;
result = Math.min(10, 20); //calling static method min by writing class name
System.out.println(result);
System.out.println(Math.max(100, 200));
}
}