Could any one explain the advantage of initializing the instance variables inside a constructor, the same thing we can do after the object has been constructed and calling the respective setters method.
3 Answers
If you rely on external code to initialize your member variables, while logically your program may be as correct as when using the constructor, you are missing the entire point of OO design.
Your goal as OO developer is to break your code into self-contained classes that appear to the outside as black boxes. External code should not have to know what kind of member variables are on the inside. The only thing external code needs to rely on the public interface of your class. And if you follow SOLID principles, you should make sure that your class exposes MINIMAL number of functions as well. This way, you provide clean abstract interface for a consumer, while inside your code can be as complex as you want it to be.
You may even get to a point where you'll decide that your class is so big that you want to break it into multiple classes and rearrange data/functions so that overall design flows better. Now imaging doing it, if you have external code which directly manipulates your data members.
Comments
Yes, you can do the same thing via setters but your class will be inconsistent until you set all required variables, moreover any one can change the variables after the setter call if setter is exposed (this may be the requirement but you can add check in setter)
The advantage of taking variables in constructor is that you can guarantee that object will be created with the valid values only.
You can consider static factories as well instead of constructors - read Item 2 - Creating and Destroying Objects from effective java.
Comments
Naive setter/getter methods are really a corruption of object oriented programming. What's the point of oo? To create classes that manage the complexity of what their subject is without code outside the class having to know about how the class works.
By adding getter/setters you are basically exposing how the class works, without actually exposing member variables, which isn't really such a good thing.
Often we do this anyway, for expediency, but really it's not the best idea.
Consider the following, you want to model a Car, and want to know when it needs an oil change. Suppose you add methods
Date getLastOilChangeDate()
void setLastOilChangeDate(Date changeDate)
int getMileageAtLastOilChange()
void setMileageAtLastOilChange(int mileage)
that basically use the values of member variables.
Seems fine right? But it really isn't good. You are exposing how a car determines whether it needs an oil change to the outside world, when it's none of the world's business how the car determines that information. Why?
Maybe a car comes out in 2015, that doesn't work this way. Perhaps it has a Oil Dirty sensor that constantly monitors the state of the oil, and bases when you need an oil change on that.
Now the above two methods have no value at all.
If you had instead added a
boolean needsOilChange()
method, the interface for the class would not have to change even tho, the implementation did.