Static Variables and Methods in Java


Static methods and variables are called without creating an instance of the class.

Source Code

class MyClass {
    static int staticVariable = 10;
    static void staticMethod() {
        System.out.println("This is a static method.");
    }
    public static void main(String[] args) {
        System.out.println(MyClass.staticVariable); // Access static variable
        MyClass.staticMethod(); // Call static method
    }
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments