5

I'm curious to know how would you explain this task I found in this quiz? Even when the getFoo method returns null, the output is still Getting Object JavaQuiz . I think it should be NullPointerException.

   public class Foo {

        static String name = " JavaQuiz";

        static Foo getFoo() {
            System.out.print("Getting Object");
            return null;
        }

        public static void main(String[] args) {
            System.out.println(getFoo().name);
        }

   }
Drunix
  • 3,313
  • 8
  • 28
  • 50
rozerro
  • 5,787
  • 9
  • 46
  • 94

3 Answers3

11

Accessing a static method or variable can be done via a null reference for the class that contains that static method/variable.

Since name is static, getFoo().name has the same result as Foo.name or just name, regardless of whether or not getFoo() returns null.

However, it is always better to use the class name when accessing a static method/variable, since it makes it clear that you intended to access a static member.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • 1
    Sir, I know this is probably not the kind of comment to write on SO. But, your knowledge is truly astounding. Always learning from your answers. :) – Debosmit Ray Apr 03 '16 at 07:46
2

static members of a class are class level Members its means there is no need of Object to access these static Members.`

It automatically loaded when classloader of jvm loads the class. So here in this case

static String name = " JavaQuiz"; //load when class get loaded by JVM class loader.

This static variable will exists in memory just after class Foo is getting loaded into the jvm.

static Foo getFoo() {  //method is also a static Member automatically loaded at Class Loading.
            System.out.print("Getting Object");
            return null;
        }

And same will be applied with this static method getFoo().

So Here System.out.println(getFoo().name);. Here is a example to abbriviate my Answer

class StaticExample
{
    static String abc ="India";
    public static void main (String[] args) throws java.lang.Exception
    {
        StaticExample obj = null;
        System.out.println("Value is ==>" + obj.abc + StaticExample.abc + abc);
    }
}

Output:-

 Value is ==>IndiaIndiaIndia

here this line of code will also produce the output.

     System.out.println(((Ideone)null).abc); // this will also print India.

Output:-

 Value is ==>India

Note:- I think there is confusion about getFoo() method but if .name makes a ambiguity. name is a static member so it can access using either className or any null reference . So, Here you may assume this scenario such that this name variable is accessed with any null reference .

Hope you Got the point.

Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
0

Disassembling (javap -c Foo) the byte code of Foo.java file reveals the under the hood information as to why you don't see a NullPointerException when the name is static.

With the above code, when disassembled it produces the following output.

With Static field

If we look at the yellow box, we see that the compiler identifies that we are trying to access a static field and so it places the getstatic instruction to get the field name. So this effectively doesn't use the instance returned from getFoo method to get the value of name field and so no NPE is raised.


If we remove the static keyword before the String name = " JavaQuiz";, it results in below disassembled code.

Without static keyword

Here we can see that java compiler instructs to use getfield instruction and this would invoked on the instance i.e., returned from getFoo method. So, this would raise a NPE if the getFoo method returns null.

So effectively, in this case, the java compiler does the magic during the compilation, i.e., if it the code is calling static field it places getstatic instruction that doesn't result in using the object reference.

We can find more information on instruction set here.