Junior — Middle
Can you explain what a static method is in object-oriented programming?
sobes.tech AI
Answer from AI
A static method is a class method that belongs to the class itself rather than to any specific instance of the class. It can be called without creating an instance of the class. In C#, static methods are declared with the static keyword.
Features of static methods:
- They do not have access to instance (non-static) fields and methods of the class.
- They can be used for operations that do not depend on instance data.
Example:
class Calculator
{
public static int Add(int a, int b)
{
return a + b;
}
}
// Call static method without creating an object
int result = Calculator.Add(5, 3);