Usage patterns for private, static, final, public, abstract keywords in java
Bozho has covered the uses for the keywords pretty well, but I will add that if you do not declare a scope at all, your scope becomes package-private, which means that anyone in the same package as the class can use that class/method. Basically, it's more permissive than private
, but less permissive than just protected
, as protected
allows access from outside a package.
Information about the 'no modifier' access here:
- http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
I recommend going through the Java tutorial:
- http://download.oracle.com/javase/tutorial/
And also take a look at the book questions if you want to explore more of Java:
- Learning Java
- https://stackoverflow.com/questions/75102/best-java-book-you-have-read-so-far
Sources tell what do these keywords mean because when/why/where they are used follows from that. My explanations have the "when" word, for example, but they follow directly from the semantics of the keywords.
private
should be used when something is not used outside of a given class- for methods and fields - when they are used only within the same class
- for classes - only on nested classes, when used in the same class
protected
should be used when- for methods and field - when you need to make them accessible to subclasses only
- for classes - again only nested classes, accessible by subcalsses
public
is used when something is accessible by every other class
The above three are "visibility modifiers". They are used when you want to limit the usage of some methods/fields/classes to a group of objects, and hide them from other objects. There is another visibility modifier - the default one (when no other is present). It is used when you want your class/method/field to be accessible only to classes from the same package.
static
is used when you don't need an instance of a class (i.e. object) to use it:- for fields - when you want to have a global field
- for methods - when you need utility functions that do not depend on object state
- for nested classes - when you want to access them without an instance of the enclosing class.
abstract
when you don't want to provide implementations in the current class:- on methods - when subclasses have to provide the actual implementation, but you want to invoke these methods (no matter how they are implemented) in this class.
- on classes - to denote that the class may have abstract methods.
final
- when you don't want something to change.- on fields, when you want to assign the value only once. It is useful when you want to pass a local variable to an inner class - you have to declare it final.
- on classes and methods - when you don't want subclasses to be able to extend / override them.
For beginners, here are my rules of thumb:
- Public: all classes should be public (this isn't quite true, but it's pretty close). For methods, think about your TV set: stuff you'd expect to do to your TV is "public".
- Private: implementation details should be private. Think about your TV set: functionality is private if the equivalent kind of thing for a TV should be private, because the user can mess the TV up permanently, get electrocuted, etc.
- Protected: ignore this for now.
- Abstract: The best example I read when learning Java was to think about "Bird". Bird is abstact, and therefore would have an "abstract" flight method. Individual species of bird know how to fly (unless they're penguins - then they throw UnsupportedOperationException).
I would strongly suggest you fight the urge to use one monolithic source file. Try to keep methods shorter than one screenful, and classes shorter than 300 lines.