Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRanvir Sahota
9,844 PointsWhy initilize variables in consturctor?
This stack overflow post: http://stackoverflow.com/questions/1994218/should-i-instantiate-instance-variables-on-declaration-or-in-the-constructor says and I quote: There is no difference - the instance variable initialization is actually put in the constructor(s) by the compiler. So why do we do it here? Is this just a coding style Craig prefers or is there another reason?
2 Answers
andren
28,558 PointsThat post is specifically about initializing instance variables whose values is not passed in to the constructor. For variables where the value you want to set them to is passed as a parameter to the constructor (like the answer
variable from the video) you do have to put them in the constructor.
And even when the values is not send to the constructor there is one difference which the post you link to points out:
You can't have exception handling with the first variant.
If you leave the initialization outside the constructor you can't handle any exceptions that might be thrown as a result of the initialization. So if there is a chance that the initialization might throw an exception for some reason then you do have to put it in the constructor.
That said if there is no real risk of an exception and the value does not come from a parameter (like the hits
and misses
variables from the video) then you are correct. There is usually no real reason to place them in the constructor. And I personally at least can say that I tend to put most of my initialization outside the constructor.
Milos Sretin
4,711 PointsAs far as I know, it is true that instance variable initialization is actually put in the constructor(s) by the compiler but there are a lot of cases when you should add that to constructor instead initializing them in class.
For example In my opinion there is no need to put some constant variables in constructor, you can initialize them in class because you know that they won't be different at any time, but... Some variables that you need to specify during class creation ( when creating class object(s)) should be placed in constructor(s).
Take a look at example from SO:
class A {
B b;
A() {
b = new B();
}
}
in this case, you probably wonder why we should initialize it in constructor. If b is always instance of B() then you can initialize it while declaring it but, as I said, there is a time when you don't know exactly what should be the instance.
Imagine if your class B is actually a Bicycle. And you don't wan't always to be Bicycle class. Putting it in constructor you can use polymorphism and pass some inherited class to it that you can type cast to class B.
Example
class A {
B b;
A(B bike) {
b = bike;
}
}
MountainBike mb = new MountainBike();
A a = new A((Bike)mb);
Ranvir Sahota
9,844 PointsRanvir Sahota
9,844 PointsThanks for your response