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 trial
Ashenafi Ashebo
15,023 PointsVariable intialization
In this video Craige initialize variables inside the Constructor: like this.answer = answer; and at the same time he initialized hits = ""; and misses = ""; My question is why he didn't initialize this.hits = hits; and this.misses = misses;
1 Answer
Seth Kroger
56,416 PointsWhen you initialize an object there can be parts set by the client or user of the object, and parts that don't need to be because they always start with the same values. answer is something the client sets because it will vary from game to game, hence why it's passed in through the constructor. hits and misses are different. Since the game is just starting and there have been no guesses yet they will always be empty strings. There is no point in passing them through the constructor, and requiring them to be is needlessly complicated for the client/user of the class.
Ashenafi Ashebo
15,023 PointsAshenafi Ashebo
15,023 Pointsthanks Seth Kroger