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 Harnessing the Power of Objects Constants

Andrew Husted
Andrew Husted
11,271 Points

2 errors: final variable assignment and String declared when no argument required

I received the following two error messages:

1.) Example.java:6: error: constructor PezDispenser in class PezDispenser cannot be applied to given types;
PezDispenser dispenser = new PezDispenser("yoda");
^
required: no arguments
found: String
reason: actual and formal argument lists differ in length

2.) ./PezDispenser.java:6: error: cannot assign a value to final variable characterName
this.characterName = characterName;
^


1- I declare the variable characterName but do not initialize it until within the PezDispenser field, isn't this permitted?

2- I state PezeDispenser takes a String value called characterName within the parentheses, but the error says no argument is required?


Example.java public class Example {

public static void main(String[] args) { // Your amazing code goes here... System.out.println("We are making a new pez despenser"); PezDispenser dispenser = new PezDispenser("yoda"); System.out.printf("The dispenser is %s %n", dispenser.getCharacterName()); }}

PezDispenser.java class PezDispenser{ final private String characterName;

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

public String getCharacterName(){ return characterName; } }

1 Answer

Chris Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

Hey Andrew,

You don't have a constructor for PezDispenser. You have:

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

But, this method is returning a String, not a PezDispenser. You just need to remove the String return type to make this method a constructor:

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

Let me know if you get any more errors after making this change!