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,955 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.