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

Terry Chia
Terry Chia
636 Points

Difference between public String and just String

Hi for the PezDispenser class, Craig uses public String getCharacterName which I don't understand why public is used because even when I removed public, I could still run Example.java and get the same result.

Also why can't we use String getcharacterName instead of getCharacterName? thanks.

1 Answer

I didnt watched the video but i can answer few of ur questions.

you want to use the keyword public in order to make this variable accessible outside the class, if u don’t use this keyword u won’t have access to this variable outside the class and u won’t be able to use it.