41

how to initialize a private static member of a class in java.

trying the following:

public class A {
   private static B b = null;
   public A() {
       if (b == null)
         b = new B();
   }

   void f1() {
         b.func();
   }
}

but on creating a second object of the class A and then calling f1(), i get a null pointer exception.

6
  • you should use a public constructor Commented Oct 29, 2009 at 8:49
  • 1
    On which line of code do you get the null pointer exception? Commented Oct 29, 2009 at 8:52
  • 1
    I think you should correct variables names, the static a is an instance of B and this is confusing, also you should post class B, maybe the NPE gets raised there. Commented Oct 29, 2009 at 8:55
  • this is just sample code. in the actual code i am using a different library. i get an error in the java.util.Collections.sort function. the stack trace shows that func has been called and some internal calls in the lib. is there a problem with the above code. Commented Oct 29, 2009 at 8:56
  • @KLE, because of some network problem this got posted twice. i'll delete the other one. Commented Oct 29, 2009 at 8:58

2 Answers 2

80

The preferred ways to initialize static members are either (as mentioned before)

private static final B a = new B(); // consider making it final too

or for more complex initialization code you could use a static initializer block:

private static final B a;

static {
  a = new B();
}
Sign up to request clarification or add additional context in comments.

9 Comments

i used a static initializer block as the constructor of B throws an exception. still i get the same error. the first call to the library function works but not the second one.
As I see it the preferred way of initialising static members depends on the actual situation. Software which creates all static members via this pattern takes a performance hit during application startup. For non-trivial situations I favor a lazy initialisation pattern for just that reason.
@rsp: You're right with your performance concern (to be correct it's not on startup but when class is loaded though - which might be the same but needn't be). I'd still consider this way of initializing static member preferred as doing initialization lazily adds complexity to the code - this should be avoided except for good reason. Performance might be one such reason.
@iamrohitbanga: consider adding more code and the stacktrace of your NullPointerException to get help.
the code would be even more confusing. i did this: if (b == null) str = "error"; str is set to "error" it seems to be an API specific problem.
|
5

Your code should work. Are you sure you are posting your exact code?


You could also initialize it more directly :

    public class A {

      private static B b = new B();

      A() {
      }

      void f1() {
        b.func();
      }
    }

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.