Wednesday, February 13, 2013

Java :: Variable Initialization

Variables initialization Advantages (In / out Constructor)

You can initialize a variable using the following:

  • Inside a constructor:

public Class Test() {
public int var;
  public Test()
{
    var = 10;
  }
}

  • Outside a constructor
public Class Test() {
         public int var = 10;
}


Advantages
Inside Constructor
Outside Constructor
One initialization even if you have multiple constructors

X
Can add exception handling to the initialization
X

More Readable

X
Faster Class Load
-
-


No major difference between both, developer use what you want!!
There is another types as static block will talk about later.

Have a nice day

No comments:

Post a Comment