tags:

views:

95

answers:

4

Hello, 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 feel 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

+2  A: 

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.

Richie_W
A: 

This similar question has a detailed answer and judging by the accepted answer, it also applies to classes.

http://stackoverflow.com/questions/714791/what-is-the-default-scope-of-a-method-in-java

Andy Shellam
+1  A: 

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.

Donal Fellows
thanks, so if i have it right - and looking at this I just found http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.htmlim better off, and it's better practice having no modifier if i want the method to be shared by two classes in a package. I just get the impression from learning OOP i should limiting public access where possible.
ivor
You should limit it, yes, but sometimes you've got to leave cracks in the armor so that everything can work. Limiting access to fields though, that's very useful anyway. (If nothing else, going through getters and setters lets you change how the field works without impacting clients of the class. Very convenient.)
Donal Fellows
A: 
  • 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.
Chris Lercher