I'm tidying up some of my code with the correct scope on some methods and attributes (I have two classes and at the moment I have a number which I just declared as public to get working, but I fee开发者_StackOverflow社区l I should look into this and make private where possible, for better practice)
When working in eclipse it's suggested on one method, when i change it private from public, that I can fix it by dropping off the scope so the method just says "static void" instead of public/private static void.
Is this a better scenario to have nothing, rather than private or public - or is the default scope equivelant to public anyway ?
Thanks
If you leave out the visiblity modifier you default to "Package Private".
This link documents the differences between each modifier. Without knowing more about your code I can't say which one you might be best off using.
The default Java scope is "package level", i.e., every other class in the same package can access the method/field, but nothing outside the package can. It's distinct from public
, protected
and private
.
- If the class is package private (not declared with public class), then having public methods won't make them more public than having no modifier.
- For interfaces, the public modifier on methods is superfluous, too (in this case, having no visibility modifier is equivalent to declaring it public). - This just for completeness, I know that you asked about static methods.
精彩评论