Junior — Middle
Can you explain what the term 'Static' means in programming?
sobes.tech AI
Answer from AI
In programming, the keyword static indicates that a class member (variable, method, property) belongs to the class itself rather than to any specific instance of the class.
This means:
- Static members exist as a single instance for the entire class.
- They can be accessed without creating an object of the class.
- They are often used to store common data or utility methods.
Example in C#:
class Calculator
{
public static int Add(int a, int b)
{
return a + b;
}
}
// Calling a static method without creating an object
int result = Calculator.Add(5, 3); // result = 8
You can also declare static fields that are shared among all objects of the class. This is useful for storing global state or counters.