2

i want to understand from the below code y the value of static variable b was not initialized and though the value was initialized in the constructor.

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

       public static void main(String[] args) {
           b.func();
       }
    }

Thanks Punith

2
  • 2
    Because you've made the initialization of b dependent upon the creation of an instance of A. Commented May 9, 2012 at 13:34
  • possible duplicate of static variable initialization java Commented Aug 16, 2012 at 20:42

4 Answers 4

7

Wrong - do it like this:

public class A {
       private static B b = null;

       static {
             b = new B();
       }

       public A() {
       }

       public static void main(String[] args) {
           b.func();
       }
    }
Sign up to request clarification or add additional context in comments.

8 Comments

It would be more helpful, I'm sure, if you could elaborate on why it's wrong to do the way in the OP posted.
But this is semantically different, right? In the code the OP posted, the field is not initialized unless an object is actually cerated.
Guys, i want to know that before the main method is called constructor will be called. so object will be initialized. or is the above statement false ??
Constructors are called for instances, not static variables.
No, constructor is not called before main(), btw you can test these things simply adding System.out.println() ;-)
|
6

You never call the A() constructor. main function is static, that means that it doesnt "belong" to an instance of A. So when you enter main, no instance of A has been created so A constructor has never been called and b is still null.

You should get a NullPointerException if you run this code.

If you add new A(); before the b.func(); then you will be okay (code will still be odd)

Comments

3

I think your question has two parts:

1) Why the value of static variable b was not initialized and though the value was initialized in the constructor?

Ans: First thing is that no constructor is called before main(). Constructors are called in main(). Whenever in main() you use new as :

public static void main(String args[]){
MyClass myclass= new MyClass()
}

then only constructor is called.

In your code static variable b was not initialized becoz u are initializing it in constructor A() but this constructor has never been called. You can call A() constructor in your code as:

public static void main(String[] args) {
          A a=new A(); // constructor gets called here.
           b.func();
       }

2) what is proper way of initializing a static variable?

The right way to initialize a static variable is to use static Initialization Blocks rather than to initialize them in constructors as shown in answer given by duffymo above.

static {
  b = new B();
 }

You can also use:

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

       public A() {
       }

       public static void main(String[] args) {
           b.func();
       }
    }

Comments

2

Your question asked to help you "understand why" the behaviour was as it was. The reason is that the constructor for Class A is not called when you invoke a static method main().

If you were to instantiate an object of type A then the constructor would be called and you reference to B initialised.

I would recomend always instantiating a Class before executing it from static void main() as a matter of good practise. If you start using a frameworks (e.g. Spring), you are better off creating instances of your classes than just writing static methods which is akin to writing procedural code.

A solution without resorting to static initialisers and following the principle outline here, is...

public class A {
    private static B b = null;

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

    public static void main(String[] args) {
        A a = new A();
        a.callFunc();
    }

    public void callFunc() {
        b.func();
    }
}

As you can see you need a way to reference the b.func() method so I have added a a.callFunc() for this reason

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.