Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
2  
Because you've made the initialization of b dependent upon the creation of an instance of A. –  user1329572 May 9 '12 at 13:34
 
possible duplicate of static variable initialization java –  nandeesh Aug 16 '12 at 20:42
add comment

4 Answers

up vote 3 down vote accepted

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)

share|improve this answer
 
yep got it... thanks –  Punith Raj May 9 '12 at 13:48
add comment

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();
       }
    }
share|improve this answer
 
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. –  posdef May 9 '12 at 13:35
2  
But this is semantically different, right? In the code the OP posted, the field is not initialized unless an object is actually cerated. –  aioobe May 9 '12 at 13:35
 
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 ?? –  Punith Raj May 9 '12 at 13:39
 
Constructors are called for instances, not static variables. –  duffymo May 9 '12 at 13:40
1  
No, constructor is not called before main(), btw you can test these things simply adding System.out.println() ;-) –  Betlista May 9 '12 at 13:40
show 4 more comments

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

share|improve this answer
add comment

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();
       }
    }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.