views:

1450

answers:

3

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.

+3  A: 

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();
      }
    }
KLE
+2  A: 

Your code looks fine. Maybe the error comes somewhere else, like inside a.func() ?

Raze
+7  A: 

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();
}
sfussenegger
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.
iamrohitbanga
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
@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.
sfussenegger
it still doesn't work
iamrohitbanga
@iamrohitbanga: consider adding more code and the stacktrace of your NullPointerException to get help.
sfussenegger
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.
iamrohitbanga
it is not always on the second instance that the problem occurs.
iamrohitbanga
to run the code i use the option -Xx900m to set the heap size, the library takes a long time to load and also takes a large amount of heap space. could it be the case that it is garbage collected before the usual time.
iamrohitbanga
@iamrohitbanga Garbage collection can't be an issue as long as you don't use fancy stuff like WeakReferences. And I don't think that more code would make anything confusing here - it's the current code that confuses people as it simply doesn't contain the source of your error. I'd suggest that you add some code and the exact stacktrace or stop bothering people with your desire for a miracle.
sfussenegger
it worked without static. there was an error in the way i was using the library. this static initialization method is right. thanks.
iamrohitbanga