Junior — Middle
What characteristics and features do static functions have in object-oriented languages?
sobes.tech AI
Answer from AI
Static functions (static methods) in object-oriented languages belong to the class, not to a specific object. This means they can be called without creating an instance of the class.
Main features:
- They do not have access to non-static fields and methods of the class, as they are not linked to a specific object.
- Often used for utility functions that do not depend on the state of an object.
- Can be called directly through the class name.
Example in Java:
public class MathUtils {
public static int add(int a, int b) {
return a + b;
}
}
// Calling a static method
int sum = MathUtils.add(5, 3);
Static methods are useful for functions that logically belong to a class but do not require data from a specific object.