xxxxxxxxxx
Use a static method
when you want to be able to access the method
without an instance of the class
xxxxxxxxxx
class scratch{
public static hey(){
System.out.println("Hey");
}
public static void main(String[] args){
hey();
//print Hey to the console
}
xxxxxxxxxx
public class Main {
public static void main(String[] args) {
// Code to be executed when the program starts
}
}
xxxxxxxxxx
public class ATM{
// Static variables
public static int totalMoney = 0;
public static int numATMs = 0;
// A static method
public static void averageMoney(){
System.out.println(totalMoney / numATMs);
}
public static void main(String[] args){
//Accessing a static variable
System.out.println("Total number of ATMs: " + ATM.numATMs);
// Calling a static method
ATM.averageMoney();
}
}