12
public class CallingStaticMethod {
public static void method() {
    System.out.println("I am in method");
}
public static void main(String[] args) {
    CallingStaticMethod csm = null;
    csm.method();
   }
}

Can someone explain how the static method is invoked in the above code?

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
java_geek
  • 17,585
  • 30
  • 91
  • 113
  • 24
    every time you write code like that a kitten dies –  Feb 08 '10 at 17:59
  • @fuzzy lollipop: :-) @OP: Years and years ago, I was bitten (and bitten hard) by this behavior, for the simple reason it had never occurred to me to call a class method using instance notation. (So I thought I was calling an instance method, and that's where the trouble began.) Avoid doing this at all costs (not that I think you're saying you want to; my take is you're confused about why it doesn't fail to compile). – T.J. Crowder Feb 08 '10 at 18:02

5 Answers5

22

It's been optimized away by the compiler, simply because having an instance of the class is not necessary. The compiler basically replaces

csm.method();

by

CallingStaticMethod.method();

It's in general also a good practice to do so yourself. Even the average IDE would warn you about accessing static methods through an instance, at least Eclipse does here.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I don't think that's it. You can call static methods using instance syntax, whether or not it can be optimized out. I'm a bit surprised by this particular formulation working, but I suppose it makes sense. :-) – T.J. Crowder Feb 08 '10 at 17:56
  • 5
    it can _always_ be optimized by the compiler, because it is static. – Bozho Feb 08 '10 at 17:59
  • Compilers can optimize code in many ways to make it run faster. Without an instance you don't need to allocate/grab heap and so on. – BalusC Feb 08 '10 at 17:59
  • 1
    @Bozho: Yeah, I suppose that's another way of looking at it. I wouldn't tend to think of it as an *optimization*, but that's just a semantic thing. – T.J. Crowder Feb 08 '10 at 17:59
  • 1
    I would reword this answer (but I'm not going to go edit it; that would be pushy): I'd say "You can use instance notation to call class methods, even though it's not a good idea. As you've discovered, the instance doesn't matter at all and can even be `null` -- it's the type of the instance variable that determines what the class is, and therefore what class method is called. Avoid doing this." – T.J. Crowder Feb 08 '10 at 18:05
  • Fair enough. I extended/reworded a bit. – BalusC Feb 08 '10 at 18:10
  • @T.J. Crowder, why not submit your own answer to the question that says that? – Yishai Feb 08 '10 at 18:11
3

Java allows you to use a Class instance to call static methods, but you should not confuse this allowance as if the method would be called on the instance used to call it.

instance.method();

is the same as

Class.method();

Dan
  • 1,763
  • 4
  • 26
  • 40
  • yes, CallingStaticMethod.method() to use the example of the question. – b.roth Feb 08 '10 at 17:56
  • That's it. There was a very distant bell ringing in my head on this; I seem to recall having been bitten by it (I *never* use instances to call class methods.) – T.J. Crowder Feb 08 '10 at 17:59
1

The java language specification says the reference get's evaluated, then discarded, and then the static method gets invoked.
15.12.4.1. Compute Target Reference (If Necessary)

This is the same behavior when you use the this reference to call a static method. this gets evaluated then discarded then the method is called.

There is even an example in the specification similar to your example.

When a target reference is computed and then discarded because the invocation mode is static, the reference is not examined to see whether it is null:

class Test1 {
    static void mountain() {
        System.out.println("Monadnock");
    }
    static Test1 favorite(){
        System.out.print("Mount ");
        return null;
    }
    public static void main(String[] args) {
        favorite().mountain();
    }
}
0

Well this is perfectly ok. The static method is not being accessed by the object instance of class A. Either you call it by the class name or the reference, the compiler will call it through an instance of the class java.lang.Class.

FYI, each class(CallingStaticMethod in the above illustration) in java is an instance of the class 'java.lang.Class'. And whenever you define a class, the instance is created as java.lang.Class CallingStaticMethod = new java.lang.Class();

So the method is called on 'CallingStaticMethod ' and so null pointer exception will not occur.

Hope this helps.

dharam
  • 7,882
  • 15
  • 65
  • 93
0

Yes we can. It will throw NullPointerException only if we are calling non static method with null object. If method is static it will run & if method is non static it will through an NPE...

To know more click here...

Sanket Dosi
  • 195
  • 1
  • 4
  • 15