Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,138 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.