1

I am have the following code

public class Sample {   
    public static void display(){
        System.out.println("Hai Sample");   
    }
    public void displays(){
         System.out.println("Hai Sample");  
    }
}

public class Sample2 {
    public static void main(String[] args) {
        Sample obj=null;
        obj.display();
        obj.displays();
        }
}

Here, When we use assign null to Sample obj I can access only static method. If I use new operator like Sample obj= new Sample(); I can access both static and non static method.

Here My question is, How object initialization happens here and How null refers Sample object's static methods and why not non static method

Kannan Thangadurai
  • 1,117
  • 2
  • 17
  • 36

5 Answers5

1

The difference is that in one case (new Sample()), you have an instance. In the second case (null), you don't.

display is a static method (related to the class, not any instance); displays is an instance method (related to a specific instance). It's a quirk of Java syntax that you're allowed to refer to static fields and methods through something that looks like an instance reference. Since the instance isn't actually used, it's not dereferenced, and so the fact it's null doesn't cause any trouble.

Your code in main is actually this:

public static void main(String[] args) {
    Sample obj=null;
    Sample.display();  // `display` is static, no instance is used
    obj.displays();    // `displays` is non-static, requires an instance
}

When you seem to call display through obj, in fact all the compiler uses the obj variable for is to see what its type is (in this case, Sample). The value of the variable (null or an instance, either way) isn't used at all, and there's no error.

In contrast, calling displays, the obj variable is used both for its type (Sample) and its value, because when calling displays the JVM needs a reference to use as this for the call. Attempting to call an instance method with a null reference is an error.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

Unlike for an instance member, you don't need an actual instance of the class to access a static member.

So, where you do:

obj.display();

this is considered by the JVM as:

Sample.display();

and will run without a problem.

An instance member, however, is depending on (and unique for each) instance of the class. It doesn't exist, untill the instance is made.

obj.display();

is code you should never write: someone who might have to maintain your code later on, might mistake it for an instance member, and assume that the impact of calling that method is limited to that instance alone, which it is not.

As for the nuts and bolts on the creation of a new instance, check the page on the topic in the official documentation.

Stultuske
  • 9,296
  • 1
  • 25
  • 37
1

Static methods can be accessed through the class, there is no need to instantiate. But you have to get an instance of the class to access no static methods.

Should view this solved question: What does the 'static' keyword do in a class?

Community
  • 1
  • 1
Wakachopo
  • 149
  • 7
1

Here My question is, How object initialization happens here and How null refers Sample object's static methods

It doesn't! null is null.

You can allways call static methods without creating any object.

Also, you should call static methods in a static way:

Sample.display();

A good IDE should give you a warning/error the way you are accessing the method:

The static method display() from the type Sample should be accessed in a static way

OH GOD SPIDERS
  • 3,091
  • 2
  • 13
  • 16
1

When -

Sample obj=null;

And you are trying to access ,

public void display() {
    System.out.println("Error");
}

It will show error. It requires an instance. But you can call this -

public static void display(){
    System.out.println("Success");   
}

As it is static , no instance is required.

Shifat
  • 732
  • 1
  • 6
  • 20