2

I was recently asked what is the output of the next code:

class MyClass {

   static void printMessage() {
      System.out.println("Hello from static");
   }

   public static void main(String[] args) {
      MyClass instance = null;
      instance.printMessage();
   }

}

Output

"Hello from static" without compile or runtime errors.

I know that static methods can be called using the name of the class such as MyClass.staticMethod() and also I know that you can call a static method inside a non-static one but the code above is confusing for me. Can anyone explain to me how is possible to call a static method using a null instance? Thanks.

gawi
  • 2,843
  • 4
  • 29
  • 44
  • 11
    Basically, because the compiler treats it as if it were written `MyClass.printMessage()` - the reference isn't used at all. – Jon Skeet Jul 27 '17 at 07:16
  • 1
    calling `static` methods on instances simply is a design flaw which can´t be removed due to backwards compatibility. It just works, because `instance` is defined as `MyClass` but the static call doesn´t mind the value assigned to it. – SomeJavaGuy Jul 27 '17 at 07:16
  • 1
    It's possible because the Java language designers decided to make that possible. It's now too late to fix that mistake, but you shouldn't call static methods that way anyway. – JB Nizet Jul 27 '17 at 07:16
  • Welcome to Stackoverflow.without instance of class we can access the static methods.MyClass.printMessage(); – Lova Chittumuri Jul 27 '17 at 07:20
  • There is no need for an instance while invoking static member or method. Since static members belongs to class rather than instance. so `instance.printMessage();` equal to `MyClass.printMessage()` – soorapadman Jul 27 '17 at 07:23
  • Hello guys. Thank you very much for all of your answers in such a short time. I'm still in the process of learning and these type of questions help me understand better what I can or can't do. Love you all and have a nice day. – George Berar Jul 27 '17 at 07:41

2 Answers2

0

You might think about instance.printMessage(); when printMessage() is static as instance.MyClass.printMessage();

The real code is:

class MyClass{

   static void printMessage(){
     System.out.println("Hello from static");
   }

   public static void main(String[] args){
        MyClass instance = null;
        instance.printMessage();
   }

 }

We can use imagination to make it easier to understand:

class MyClass{

   static void printMessage(){
     System.out.println("Hello from static");
   }

   public static void main(String[] args){
        MyClass instance = null;
        instance.MyClass.printMessage();
   }

 }
luke
  • 3,435
  • 33
  • 41
0

You might have got a warning at this line instance.printMessage(); saying

The static method printData() from the type CallingStaticMethodOnNullReference should be accessed in a static way

So while compiling even if the method is called with or without Class name, the compiler will add the Class name along with the method call in the .class file like MyClass.printMessage();. That's why you are not getting any Exception. Sometimes code manipulation will be better if you think in compiler's perspective.

Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52