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

I had a problem While declaring the variable characterName as final variable

public class PezDispenser{ final private String characterName; public void PezDispenser(String characterName){ this.characterName = characterName; } public String getCharacterName(){ return characterName; } }

console: error: cannot assign a value to this.characterName = characterName;

Brandon Kelly
Brandon Kelly
3,808 Points

Its a bit hard to tell from the post formatted as it is, and I'm still a newb, but if your post reads the same as your code, try placing final after private. Again I'm no expert(for sure!), but it seems like public or private are always the first definitions in the line. Ex.: private final String getCharacterName; instead of final private String getCharacterName; .

Hope this helps. If somebody with more experience finds this advice to be untrue, please correct me.

2 Answers

thank you, Brandon Kelly, but the problem is the void that i had add it to the constructor "public void PezDispenser(){.....}" when i removed "void" it seems to work.

Brandon Kelly
Brandon Kelly
3,808 Points

Glad you got it! Just out of curiosity, did it work with final before private? I've never tried that before. I'm still learning the basics of the language and sometimes it's hard to tell what is necessary and what is just common practice.

it is the same thing whether private before final or final before private. it doesn't cause a problem

Brandon Kelly
Brandon Kelly
3,808 Points

Cool. Thanks! This is what makes this forum great. I tried to help and ended up learning myself. Good luck in your Java studies!