Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 934499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:00:41+00:00 2026-05-15T21:00:41+00:00

I wrote this program in Java public class Why { public static void test()

  • 0

I wrote this program in Java

public class Why {

  public static void test() {
    System.out.println("Passed");
  }

  public static void main(String[] args) {
    Why NULL = null;
    NULL.test();
  }

}

I read that invoking a method on a null object causes NullPointerException, and yet the above program doesn’t? Why is this? Am I not understanding something correctly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:00:42+00:00Added an answer on May 15, 2026 at 9:00 pm

    test() is a static method. A static member belongs to the type, and do not require an instance to access.

    A static member should ONLY be accessed via a type expression. That is, you should’ve written it as follows:

    Why.test(); // always invoke static method on the type it belongs to!
    

    Java does allow you to access a static member via an object reference expression, but
    this is VERY misleading, since this is NOT the actual semantics of a static member access.

    Why aNull = null; 
    aNull.test(); // DO NOT EVER DO THIS!
    // invokes Why.test(), does NOT throw NullPointerException
    

    When accessing a static member through an object reference expression, only the declared type of the reference matters. This means that:

    • It doesn’t matter if the reference is actually null, since no instance is required
    • If the reference is not null, it doesn’t matter what the runtime type of the object is, there is no dynamic dispatch!!!

    As you can see, the exact opposites are true on both points for instance member access. This is why static members should NEVER be accessed in a “non-static” way, because it gives a very misleading appearance on what it’s actually doing.

    Related questions

    • Why doesn’t Java allow overriding of static methods ? (understanding this is crucial!)
    • Why isn’t calling a static method by way of an instance an error for the Java compiler?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import java.lang.*; import java.io.*; class test { public static void main(String[] a) throws Exception
CODE import java.io.*; class tester { public static void main(String args[]) { try {
Here's a sample Java class that I wrote public class Sleeper { public static
I wrote a Multithreaded program in Java given below :- public class Client {
I wrote this short program to learn the javax.sound.midi system. This is using Java
When I write this in the main method: System.out.println(Hello); Nothing is outputted on the
I wrote this small C++ program and built it(Release) #include<iostream> int main(){ std::cout<<Hello World;
I wrote this simple Java program which connects to internic server and returns the
I am new to Java Multithreading World, I wrote this program, I just wanted
I wrote a java program to test the capabilities of processors by running an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.