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
chrisfinelli
1,161 PointsSlight constructor confusion... In the Hangman's Game.java file
Hello. I have slight constructor confusion... In the Hangman's Game.java file, there is a Game constructor used but the following statements don't use the the "new" java operator to create an object. Also, I'm not sure where it was stated in the series that the use of double quotes "" is an accepted shortcut for declaration, instantiation, or initialization. Please see below & thanks in advance
class Game {
public static final int MAX_MISSES =7;
private String answer;
private String hits;
private String misses;
public Game(String answer) {
this.answer = answer;
hits = "";
misses = "";
}
2 Answers
Steve Hunter
57,712 PointsHi Chris,
The double-double quotes is used to create an empty string. You can add characters within them to assign a string value to the variable.
Here, you've declared the string variable hits in the class with:
private String hits;
In the constructor you assign a value to it. With some datatypes, you can assign literals to them rather than going the long-hand way of creating objects. Items such as char, String, int etc. can be assigned literals to short-cut the object creation:
String name = new String("Steve");
// or
String name = "Steve";
With your Game constructor, this gets called when you create a new Game with:
Game game = new Game("treehouse");
So, game will then hold treehouse within its answer member variable and have two initialized string variables holding blank strings.
I hope that helps - ask about anything I have missed.
Steve.
chrisfinelli
1,161 PointsThanks for breaking it down for me, Steve! Great explanation :)