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

During the Constants course, my code for the max pez count isn't working.

class PezDispenser { public final int MAX_PEZ = 12; final private String characterName;

public PezDispenser(String name) { this.characterName = characterName; }

public String getCharacterName() { return characterName; }

public String swapHead(String characterName) { String originalCharacterName = this.characterName; this.characterName = characterName; return originalCharacterName; } }

This is the error report I get while trying to open the PezDispenser.java file:

                                                                                                                                                                 jshell> /open PezDispenser.java                                                                                                                                  |  Error:                                                                                                                                                        |  cannot assign a value to final variable characterName                                                                                                         |      this.characterName = characterName;                                                                                                                       |      ^----------------^                                                                                                                                        |  Error:                                                                                                                                                        |  variable characterName might not have been initialized                                                                                                        |      this.characterName = characterName;                                                                                                                       |                           ^-----------^                                                                                                                                                                                                                                                                                         jshell>

3 Answers

Ah - I see you are trying to change the characterName in your swapHead method. (Forgive me, I couldn't find the video that this relates to.) You cannot change a final field once it has been initalized - you can only set it once. If you remove the "final" from characterName your code should work okay.

class PezDispenser { public final int MAX_PEZ = 12; private String characterName;

That did the trick! Thank you so much Steven!

Cool :)

You are calling the argument in your constructor 'name' instead of characterName

so to make it work you can try:

class PezDispenser { public final int MAX_PEZ = 12; private final String characterName;

public PezDispenser(String characterName) { this.characterName = characterName; }

...

Hey Steven, thanks for your answer. I wound up going back to the first video and building the code form scratch as he explains each thing. Once again though when I got to this point my code doesn't work. I even put in what you said in your comment but I'm just getting the same error:

class PezDispenser { public final int MAX_PEZ = 12; final private String characterName;

public PezDispenser(String characterName) { this.characterName = characterName; }

public String getCharacterName() { return characterName; }

public String swapHead(String characterName) { String originalCharacterName = this.characterName; this.characterName = characterName; return originalCharacterName; } }

treehouse:~/workspace$ javac Example.java                                                                                                                        treehouse:~/workspace$ jshell                                                                                                                                    |  Welcome to JShell -- Version 9-ea                                                                                                                             |  For an introduction type: /help intro                                                                                                                                                                                                                                                                                                                                                                                                                                                           jshell> /open PezDispenser                                                                                                                                       |  File 'PezDispenser' for '/open' is not found.                                                                                                                                                                                                                                                                                  jshell> /open PezDispenser.java                                                                                                                                  |  Error:                                                                                                                                                        |  cannot assign a value to final variable characterName                                                                                                         |      this.characterName = characterName;                                                                                                                       |      ^----------------^