13

Since static methods can be called directly from the class (i.e. ClassName.methodName), why it is required to call a static method with the object of the class?

If someone knows then, elaborate with example.

public static void methodA(){

}
4
  • 1
    have you tried it yet??? Commented Jul 17, 2014 at 9:56
  • 1
    Did you try ((YourClass)null).methodA()? Commented Jul 17, 2014 at 9:56
  • 3
    "...then why it is required to call static method with object..." -- it is not required. Commented Jul 17, 2014 at 9:56
  • @Kayaman : You are right its running well. Commented Jul 19, 2014 at 11:28

4 Answers 4

23

The following code contains an example, in which a static method is called via a null reference.

public class Test {
    public static void main(String... args) {
        Test test = null;
        test.greeting(); // call with null reference
    }
    public static void greeting() {
        System.out.println("Hello World");
    }
}

Because Test::greeting is a static method, the expression test.greeting() is identical to Test.greeting(). For that reason, there is no NullPointerException thrown at runtime.

Sign up to request clarification or add additional context in comments.

7 Comments

sir, could please explain why NPE is not throwing ?
@LMK Attached the full reason in my post which is explained in spec. Please have a look.
This is fairly self explanatory. The call to greeting is not using the instance (which is null) so no NPE occurs.
If i change static method to instance method above, then it will throw NullPointerException.
String s = null; System.out.println(""+s.trim()); throws NPE why ?
|
11

There is no need for an instance while invoking static member or method.

Since static members belongs to class rather than instance.

Example 15.11.1-2. Receiver Variable Is Irrelevant For static Field Access

The following program demonstrates that a null reference may be used to access a class (static) variable without causing an exception:

The example from spec it self.

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

And the analysis of why it is happening

Even though the result of favorite() is null, a NullPointerException is not thrown. That "Mount " is printed demonstrates that the Primary expression is indeed fully evaluated at run time, despite the fact that only its type, not its value, is used to determine which field to access (because the field mountain is static).

4 Comments

String s = null; System.out.println(""+s.trim()); throws NPE why ?
@LMK trim() is not a static method here :) it needs specific instance to perform trim operation.
@LMK There is no silly questions. Only silly people are there who afraid to ask a question :) Happy coding.
Perfect answer using exact reference! That's what I was looking for. This should be marked as correct answer to the question.
10

Very well you can call a static method with null object.

See the example below.

public class Hashing {

    public static void Hash() {
        System.out.println("hello");
    }

    public static void main(String[] args) {
        Hashing h = null;
        h.Hash();
    }
}

Above code snippet will print hello

Because at the compile time h.hash() will be converted to Hashing.hash() since hash() is a static method.

When I de-compiled .class file I got this code.

/*
 * Decompiled with CFR 0_114.
 */
import java.io.PrintStream;

public class Hashing {
    public static void Hash() {
        System.out.println("hello");
    }

    public static void main(String[] args) {
        Object h = null;
        Hashing.Hash();
    }
}

As you can see in the above snippet h.Hash(); is converted to Hashing.Hash();

HTH!!

1 Comment

perfect explanation! :thumbsup
0

Yes,and this is a strange situation. Below is the example.

public class Test{
    public static String foo(){
     System.out.println("foo");
     return "";
    }

    public static void main(String args[]){
            Test t=null;
            t.foo();//no error will be thrown
    }
}

Output: foo

Explanation : it is expected this should throw a null pointer exception but it just gives you a warning that "The static method foo() from the type Test should be accessed in a static way". But when executing it will work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.