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 Methods

Andrada Terenche
Andrada Terenche
1,103 Points

Isn't public type predefined?

Do we necessarily need to specify the public type? Isn't it public by default? public String getCharacterName() { } Can't we just say String getCharacterName() {}?

2 Answers

Thomas Nilsen
Thomas Nilsen
14,957 Points
String getCharacterName() {} //This is package-private 
public String getCharacterName() { }

More info here

Abhishek Upadhyay
Abhishek Upadhyay
2,986 Points

HI Andrada Terenche

No it is not.

By default if you don't declare the access type the access scope is only restricted to the folder where classes in the same folder can access it without any issue. Public makes it accessible to the world (anyone) hence it needs to be put specifically.

Yep, but I'm gonna use narrow restriction. In this exercise the getter has contract only with other object in this same package. We don't want to break this contract by other objects outside the package right now, so our main class (enter the app) is like facade. It means this app is more safe and better to maintain.