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 8904091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:01:57+00:00 2026-06-15T02:01:57+00:00

Possible Duplicate: How come invoking a (static) method on a null reference doesn’t throw

  • 0

Possible Duplicate:
How come invoking a (static) method on a null reference doesn’t throw NullPointerException?
Static fields on a null reference in Java

I tried the code from this old video:

class Impossible {
    public static void main(String[] args) {
        Thread t = null;
        System.out.println(t.currentThread().getName());
    }
}

Output: main

Well, just what the heck is that?! Does java.lang.Thread breach any the NullPointerException rule?

But what I’m most interested in it: How can I make that variable behave to throw a NullPointerException?

  • 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-06-15T02:02:04+00:00Added an answer on June 15, 2026 at 2:02 am

    Does java.lang.Thread breach any the NullPointerException rule?

    No, reason for a NPE been thrown is not related to a class. It is related to an instance of that class, on which invocation is done. Also, it depends upon which type of method, or field you are accessing.

    What is happening here is, currentThread() is a static method of Thread class. Which is boudn to a class, rather than an instance. So, even if you invoke it on a reference of Thread class, it is actually invoked on class name.

    So,

    Thread t = null
    t.currentThread();
    

    is actually invoked as: –

    Thread.currentThread();
    

    So, 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 pointing to null, since no
      instance is required.

    • If the reference is not null, it doesn’t matter what the type
      of the object the reference is pointing to, there is no dynamic dispatch.


    How can I make that variable behave to throw a NullPointerException?

    Well, the current print statement will never throw a NPE. The first part is already explained above. Now, let’s move ahead.

    Thread.currentThread();
    

    The above invocation will never return null. It always returns the current thread instance. And in Java, you are always inside one or the other thread. Even when inside public static void main method, you are executing the Main Thread. So, currentThread can’t be null.

    And hence, further invocation: –

    Thread.currentThread().getName();
    

    will work fine, and return the name of current thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: get methodinfo from a method reference C# This is most likely something
Possible Duplicate: How come a non-const reference cannot bind to a temporary object? This
Possible Duplicate: Why can't you reduce the visibility of a method in a java
Possible Duplicate: Reference - What does this symbol mean in PHP? I've come accross
Possible Duplicate: How come I can't remove the blue textarea border in Twitter Bootstrap?
Possible Duplicate: C# Why can equal decimals produce unequal hash values? I've come across
Possible Duplicate: Where does ‘Hello world’ come from? What was the first Hello World
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: How come MD5 hash values are not reversible? I was reading a
Possible Duplicate: C: How come an array’s address is equal to its value? Could

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.