static Keyword Revisited in Java


The static keyword can be applied to variables and methods. A static member belongs to the class rather than instances of the class.

Source Code

public class Calculator {
    public static int add(int a, int b) {
        return a + b;
    }
}

int sum = Calculator.add(5, 3); // No need to create an instance to call the add method

Use static members for utility or helper methods and constants that don’t require object state.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments