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 Meet Objects Creating Classes

file does not contain class PezDispenser

l keep getting this error and on refreshing l realise that an Example class has been generated but there is none for PezDispenser.

Example.java

public class Example {

public static void main(String[] args) { // Your amazing code goes here... System.out.println("Lets see if this changes anything"); PezDispenser dispenser = new PezDispenser(); System.out.printf("The dispenser is %s %n", dispenser.characterName); }

}

PezDispenser.java

class PezDispenser{ String characterName = "Yoda"; }

2 Answers

Can u explain which error u get again? it isnt clear.

Itsa Snake
Itsa Snake
3,852 Points

I get the same error. It seems it can't read the PezDispenser file within the same path. I have definitly saved it down!

./PezDispenser.java:1: error: class, interface, or enum expected
Class PezDispenser {
^
./PezDispenser.java:4: error: class, interface, or enum expected
}
^
Picked up JAVA_TOOL_OPTIONS: -Xmx128m
Picked up _JAVA_OPTIONS: -Xmx128m
./PezDispenser.java:1: error: class, interface, or enum expected
Class PezDispenser {
^
./PezDispenser.java:4: error: class, interface, or enum expected
}
^
Picked up JAVA_TOOL_OPTIONS: -Xmx128m

Picked up _JAVA_OPTIONS: -Xmx128m
./PezDispenser.java:1: error: class, interface, or enum expected
Class PezDispenser {
^
./PezDispenser.java:4: error: class, interface, or enum expected
}
^
Example.java:6: error: cannot access PezDispenser
PezDispenser dispenser = new PezDispenser();
^
bad source file: ./PezDispenser.java
file does not contain class PezDispenser
Please remove or make sure it appears in the correct subdirectory of the sourcepath.
3 errors
treehouse:~/workspace$

Hey JJ Jar, I found out what is causing your errors, and it's one of those simple ones. You capitalized the word "Class" at the beginning of your PezDispenser.java file. Keep the word lower case and it will work for you.