views:

196

answers:

11

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

+11  A: 

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

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

Kurt
But they can be called on (non-null) instances, with the same effect. (Though this is not preferable, as it can be deceptive.)
FarmBoy
@FarmBoy: they even can be called on _null_ instances with the same effect, compiler will optimize it for you )
Roman
Exactly. How is it possible? What is happening underneath.
Sai Praveen
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
+1  A: 

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

fastcodejava
Because they are in the same class **and the two methods are both static...**
akf
+2  A: 

Try:

Test.test();
Binary Nerd
+5  A: 

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
A: 
class Test { 
  public static void test() { 
    System.out.println("hi"); 
  } 

  public static void main(String[] args) { 
    Test.test();
  }
}
Tim Drisdelle
+3  A: 

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
A: 

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
Test.test() works fine. But the code snippet above also works. I wanted to know how it works
Sai Praveen
It is not considered good practice to use an instance to refer to static methods. Use Test.test() not myClass.test().
jax
A: 

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

Adeel
As the main method is in the same class **and they are both static**...
akf
A: 

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
@Roman answerer to your question.
St.Shadow
+1  A: 
for (Method m : Class.forName ("Test").getDeclaredMethods ()) {
   if (Modifier.isStatic (m.getModifiers ()) {
      m.invoke (null);
   }
}

just for lulz

Roman
Cool! I like it! :)
St.Shadow
A: 

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