Static Member Function in Java


The static keyword is used on a class, method, or field to make them work independently of any instance of the class.Static fields are common to all instances of a class. They do not need an instance to access them. Static methods can be run without an instance of the class they are in. However, they can only access static fields of that class.

Source Code

class Mathematical {
    public static int power(int base, int power) {
        int result = 1;
        for (int i = 1; i <= power; i++) {
            result *= base;
        }
        return result;
    }
}
public class staticExample {
    //Static Member Function in Java
    public static void main(String args[]) {
        System.out.println("Power : " + Mathematical.power(2, 3));
    }
}
 

Output

Power : 8
To download raw file Click Here

Basic Programs