abhi1489
abhi1489

Reputation: 207

Can we initialise static variable inside constructor?

I have the following code snippet:

class Constructor {

  static String str;

  public void Constructor() {
      System.out.println("In constructor");
      str = "Hello World";
  }

  public static void main(String[] args) {
      Constructor c=new Constructor();
      System.out.println(str);
  }
}

Its output is null even though the string is initialized inside the constructor.

Why is that so?

Upvotes: 6

Views: 11346

Answers (6)

Kanad
Kanad

Reputation: 11

If we are using static variable then we don't have need to create an object and also a constructor which is used to initialize value of instance variable on place of default values. We can use static block to initialize static variable which always executes first before the main() method in Java.

Upvotes: 1

Vishal
Vishal

Reputation: 714

Yes you can. But you should not.

Let me elaborate it in detail.

When you are initializing its value inside constructor; you are actually assigning some value to it. This will always be assigned whenever you create object of your class (which you not what you intended to do when you thought of initializing static data member right).

Here static blocks comes to rescue us.

Static blocks are executed whenever class is loaded into memory by JVM. Hence once your class is loaded, whichever assignments you have defined inside your static block happens for the first and last time until your class is there in memory.

Hence its ideal to use static initializer blocks always to initialize static data members.

In your case introduce below code snippet inside your class

static {
    str = "Hello World";
}

Upvotes: 0

MichaelK
MichaelK

Reputation: 3083

Also as a side-note: you may use a static-block to initialize static variables. In your case like so:

static {
    str="Hello World";
}

This means you only initialize the variable once and not every time the constructor is executed.

Upvotes: 3

user3437460
user3437460

Reputation: 17454

I have the following code snippet whose output is null even though the string is initialised inside the constructor

A constructor do not have return type. Your current so called constructor comes with a return type of void hence it is no longer a constructor, but a method of your class.

Try again with:

public Constructor(){  //void removed
    System.out.println("In constructor");
    str="Hello World";
}

Upvotes: 1

SpringLearner
SpringLearner

Reputation: 13844

As I mentioned in the comments public void Constructor(){ is not a constructor because constructors do not have return type.As your Constructor is of void so its not an constructor

Remove the void keyword

class Constructor {

static String str;

public Constructor(){
    System.out.println("In constructor");
    str="Hello World";
}

public static void main(String[] args) {
    Constructor c=new Constructor();

    System.out.println(str);

}


}

output:Hello World

Upvotes: 6

Maljam
Maljam

Reputation: 6274

public void Constructor() is not a constructor.. it's a void method. If you remove the void, it should work as intended

Upvotes: 10

Related Questions