2

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

Sai Praveen
  • 79
  • 1
  • 3
  • Could you reformat your question using the "code" format? – Timothy Feb 10 '10 at 13:11
  • 4
    Note that your code would have worked if you changed `Test t = null;` to `Test t = new Test();` – Pool Feb 10 '10 at 13:17
  • 1
    @The Feast: it works even now, see my comment to the top-ranked answer. – Roman Feb 10 '10 at 13:41
  • Wow, I never knew that - in this case the compiler optimizes in a way that possibly alters the flow. – Pool Feb 10 '10 at 14:00
  • Have a look at this question as well: http://stackoverflow.com/questions/2223386/why-doesnt-java-allow-overriding-of-static-methods/2223408#2223408. Calling a static method through an instance is just a syntactic trick. They should have forbidden that, it brings only confusion. – ewernli Feb 10 '10 at 14:02
  • Or in other words: `((Test)null).test()`; – flybywire Feb 10 '10 at 14:23

11 Answers11

11

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

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

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Kurt
  • 4,477
  • 2
  • 26
  • 34
  • But they can be called on (non-null) instances, with the same effect. (Though this is not preferable, as it can be deceptive.) – Eric Wilson Feb 10 '10 at 13:25
  • 1
    @FarmBoy: they even can be called on _null_ instances with the same effect, compiler will optimize it for you ) – Roman Feb 10 '10 at 13:35
  • Exactly. How is it possible? What is happening underneath. – Sai Praveen Feb 10 '10 at 13:53
  • There are some tools for decompiling byte code exist. You can look what happens. I don't think that you can find something related in spec. But compiler simply replaces all instances with class name every time when you call static method via instance of a class. – Roman Feb 10 '10 at 13:59
5

You don't need to instantiate Test for calling a static method. Your main could be look like this:

public static void main(String[] args) {
    Test.test();
}
Mnementh
  • 50,487
  • 48
  • 148
  • 202
3

Static methiods should be invoked with the class name, without the need for creating an instance of the class, as in

ClassName.methodName(args);

or

methodName(args); // from other static methods of the same class.

You can also refer to static methods with an object reference like

instanceName.methodName(args)

but this is discouraged because it does not make it clear that they are class methods.

So in your case:

Test.test();

or

test();

from the main method will do.

codaddict
  • 445,704
  • 82
  • 492
  • 529
2

Try:

Test.test();
Binary Nerd
  • 13,872
  • 4
  • 42
  • 44
1

You are in the same class, you can simply call test() from main().

fastcodejava
  • 39,895
  • 28
  • 133
  • 186
  • 2
    Because they are in the same class **and the two methods are both static...** – akf Feb 10 '10 at 13:41
1
for (Method m : Class.forName ("Test").getDeclaredMethods ()) {
   if (Modifier.isStatic (m.getModifiers ()) {
      m.invoke (null);
   }
}

just for lulz

Roman
  • 64,384
  • 92
  • 238
  • 332
0
class Test { 
    public static void test() { 
        System.out.println("hi"); 
    } 

    public static void main(String[] args) { 
        Test.test();
    }
}
CSchulz
  • 10,882
  • 11
  • 60
  • 114
Timothy
  • 2,457
  • 19
  • 15
0

The good thing about static methods and static variables is that you do not need an instance of the class to use it.

Normally you would create an instance and call the method

Test myClass = new Text();
myClass.test();

However with static methods the first line is not necessary, You just need to write the Class name at the start

Test.test();

However, in static methods you are not able to access any instance variables inside the Test class - unless they are also static!

jax
  • 37,735
  • 57
  • 182
  • 278
  • Test.test() works fine. But the code snippet above also works. I wanted to know how it works – Sai Praveen Feb 10 '10 at 13:29
  • It is not considered good practice to use an instance to refer to static methods. Use Test.test() not myClass.test(). – jax Feb 11 '10 at 08:45
0

Call Test.test(). As the main method is static and in the same class so you can also directly call test() too.

Adeel
  • 19,075
  • 4
  • 46
  • 60
0

By the way. The code works fine without any nullpointerexception This code prints hi

I wanted to know what happens internally when a reference is used to invoke a static method.

Sai Praveen
  • 79
  • 1
  • 3
0

It works because when invoking a static method using a reference, the reference is not used. The compiler looks at the declared/static/compile-time type of the expression the method is being called on, and uses that type to find the static method.

You gain nothing by calling a static method on a variable, and you can confuse people who think a polymorphic call is occurring.

ILMTitan
  • 10,751
  • 3
  • 30
  • 46