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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:26:02+00:00 2026-05-26T18:26:02+00:00

The following simple code snippet is working fine and is accessing a static field

  • 0

The following simple code snippet is working fine and is accessing a static field with a null object.

final class TestNull
{
    public static int field=100;

    public TestNull temp()
    {
        return(null);
    }
}

public class Main
{
    public static void main(String[] args)
    {
        System.out.println(new TestNull().temp().field);
    }
}

In the above code, the statement System.out.println(new TestNull().temp().field); in which the static field field is being associated with a NULL objectnew TestNull().temp(), still it is returning a correct value of it which is 100 instead of throwing a null pointer exception in Java! Why?

  • 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-26T18:26:03+00:00Added an answer on May 26, 2026 at 6:26 pm

    As opposed to regular member variables, static variables belong to the class and not to the instances of the class. The reason it works is thus simply because you don’t need an instance in order to access a static field.

    In fact I’d say it would me more surprising if accessing a static field could ever throw a NullPointerException.

    If you’re curious, here’s the bytecode looks for your program:

    // Create TestNull object
    3: new             #3; //class TestNull
    6: dup
    7: invokespecial   #4; //Method TestNull."<init>":()V
    
    // Invoke the temp method
    10: invokevirtual   #5; //Method TestNull.temp:()LTestNull;
    
    // Discard the result of the call to temp.
    13: pop
    
    // Load the content of the static field.
    14: getstatic       #6; //Field TestNull.field:I
    

    This is described in the Java Language Specification, Section 15.11.1: Field Access Using a Primary. They even provide an example:

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

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

    It compiles, executes, and prints:

    Mount Chocorua

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

Sidebar

Related Questions

I have the following simple Java code: package testj; import java.util.*; public class Query<T>
In emacs, I've read the following code snippet in simple.el : (frame-parameter frame 'buried-buffer-list)
Let's look at the following simple code snippet in Java. interface Sum { abstract
Let's look at the following code snippet in Java. package demo; import java.util.Calendar; final
I have following simple code snippet: StringBuilder sb = new StringBuilder(); Locale l =
The following simple code snippet uses the interface Named containing two methods namely name()
I need a simple code snippet which can do the following. I need a
I've narrowed the problem down to the following simple code snippet: #!/usr/bin/env ruby print
i have the following code snippet, in an appliation working with Zend Framework. I
Very simply put, I have the following code snippet: FILE* test = fopen(C:\\core.u, w);

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.