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

Java Java Objects (Retired) Creating the MVP Storing Guesses

What is the reason behind initializing the private variables in the constructor?

private String mHits;
private String mMisses;

public Game(String answer)
mAnswer = answer;
mHits = "";      
mMisses = ""; 
\\ Why have we initialized the private variables here in the constructor??


2 Answers

Harry James
Harry James
14,780 Points

There isn't a specific reason for initializing the variables in the constructor and, in fact, you could initialize them when you first declare the variables and the code would run fine (But you must still set mAnswer to the answer passed into the constructor!)

I believe Craig just did this to keep everything together but, it's up to you how you want to organise it!

Hope it helps and if you have any more questions, give me a shout :)

Thanks Harry. That helps !! :)

Greg Kitchin
Greg Kitchin
31,522 Points

I think it's just best practice to be honest. You want to keep everything relevant together, especially with the idea of separation of concerns.