Static Member Function in Java
Static Member
- A static member function belongs to the class itself, not to any specific object (instance) of the class.
- You can call a static function without creating an object of the class.
- Static functions cannot access non-static members (variables or functions) directly because they do not belong to any instance.
Code Snippet
//Static Member Function in Java
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 App {
// Static Member Function in Java
public static void main(String[] args) {
System.out.println("Power : " + Mathematical.power(2, 3));
}
}