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

  • Home
  • SEARCH
  • 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 6825965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:05:34+00:00 2026-05-26T22:05:34+00:00

I have the following classes: public class A { static { B.load(A.class); } public

  • 0

I have the following classes:

public class A {

    static {
        B.load(A.class);
    }

    public static final C field1 = new C("key1", "value1");

    public static final C field2 = new C("key2", "value2");

    public static void main(String[] args) {
        System.out.println(A.field1);
    }

}

and

public class B {

    public static void load(Class<?> clazz) {
        for (Field field : clazz.getFields()) {
            try {
                System.out.println("B.load -> field is " + field.get(null));
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

}

and

public class C {

    private final String key;

    private final String value;

    public C(String key, String value) {
        super();
        this.key = key;
        this.value = value;
    }

    public String getKey() {
        return this.key;
    }

    public String getValue() {
        return this.value;
    }

    @Override
    public String toString() {
        return "C [key=" + this.key + ", value=" + this.value + "]";
    }

}

When A is executed I get:

B.load -> field is null
B.load -> field is null
C [key=key1, value=value1]

Why does field.get(null) return a null value when it is executed? I get no exception and it seems that this behavior is not explained by the Javadoc.

  • 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-26T22:05:34+00:00Added an answer on May 26, 2026 at 10:05 pm

    I believe all static members will be executed (for static blocks) and initialized (for static fields) in declaration order. Try placing the static block of class A at the end of the class, or at least after the static fields. Lemme know if that makes a difference.

    EDIT: info regarding the behaviour for primitives and Strings…

    When you have a final static field that is a primitive or a String literal (or an expression that can be statically evaluated that results in one of these), it’s considered a compile-time constant. Basically, setting such a value does not require “computation”, like invoking a constructor or evaluating vis-a-vis other fields that may not have been initialized yet. Although Strings aren’t primitives, they’re given a special treatment in compilation to make String literals in code a possibility.

    This means such fields can be assigned as soon as a class has been loaded and is ready for initialization. Although I don’t know the specification details regarding that, experimenting with reflection shows that this is what must be happening.

    Following sections of the Java language specification are relevant to understanding this behaviour:

    • Restrictions on the use of fields during initialization. Reflection lets you get to these fields “sooner” than they appear in source code order. If you tried using them directly in a static block that appears before the field declaration you’d get an error regarding an illegal forward reference from the compiler.
    • Initialization of classes and interfaces.
    • What constitutes a constant expression.
    • Final variables. That last paragraph of this section is quite relevant and implies more than its vagueness lets on.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following classes: public class Obj<T> extends BaseModel { public static final
I have the following classes: public class DictionaryBuilder<T> where T : IDictionary<string, object>, new()
Suppose I have the following classes: public class Test { public static void main(String[]
I have the following classes: MinMaxArray class: public class MinMaxArray { public static <T>
Suppose, I have following classes: public class DisposableObj : IDisposable { public ChildObj CreateObj();
I have the following classes: public class Person { public String FirstName { set;
I have the following classes: Public Class Email Private Shared ReadOnly EMAIL_REGEX = \b[a-zA-Z]+[a-zA-Z0-9._+-]+@
I have the following classes: public class Entity<T> where T : Entity<T> { public
I have the following classes public interface InterfaceBase { } public class ImplementA:InterfaceBase {
I have the following two classes: public class Address { public string AddressLine1 {

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.