Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does the following code work

Currently I'm trying to invoke it like this:

class Test {
    public static void test() {
        System.out.println("hi");
    }
    public static void main(String[] args) {
        Test t = null;
        t.test();
    }
}

The output of the code is hi

like image 258
Sai Praveen Avatar asked Jul 05 '26 10:07

Sai Praveen


1 Answers

Try Test.test() with the class name before the dot.

Static methods are called on the class itself not instances of the class.

like image 193
Kurt Avatar answered Jul 07 '26 00:07

Kurt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!