Modifiers
In Java, modifiers can be used in class, variable (inc constants), or method declarations.
Access Modifiers
Java has four access levels. In order from least to most restrictive, they are:
public Any code anywhere can access the public thing.
protected Works like default (code in the same package has access), except it also
allows subclasses outside the package to inherit (but not reference) the
protected thing.
default Only code within the same package as the class with the default thing can
access the default thing (some books call this package level access).
private Only code within the same class can access the private thing. This means
private to the class, not private to the object. One Dog can see another Dog
object's private stuff, but a Cat can't see a Dog's privates. (from HFJ).
Java has three access modifiers to define access level: public, protected, and private. If you don't
use an access modifier you get the default level.
It is good practice to reduce the degree of access to the minimum; it reduces the number of
possible interactions and makes it easier to spot errors when they do occur.
All four access levels can be applied to members of a class (including constructors).
Interface methods and constants are always public whether the public modifier is used or not.
A class can be either public or default. Local variables, either of method or block scope, cannot
have an access modifier applied.
Static Modifier
Any variable declared immediately inside a class (ie not local to a method) can be declared as
static.
eg private static model;
There is only one copy of a static variable of a class. All objects of that class share the same
variable; whereas with non-static variables each object has its own separate copy. Static variables
are normally referred to using the class name rather than the object name, although using the
object name does compile and you may see this in the SCJP exam. Static variables are useful for
situations where data needs to be shared among different objects.
The final modifier is used in conjunction with static to define a constant (the final modifier
means that the value cannot be changed):
eg public final static double PI = 3.14159;
A method may also be declared as static:
eg public static double sqrt(double num)
A static method is prohibited from referencing any non-static variables or methods, ie it can only
access the parameters it is passed or static variables. This however has the advantage that a static
method can be called without instantiating an object. Usually static methods are called using
their class name.
eg double rand = Math.random();
Static methods can also be called using the name of an object of its class, though often there is no
need to instantiate an object, eg the Math class is never instantiated as all its methods are static.
Note that the main method with which every Java application begins execution is static. This is
why you cannot access instance variables (object attributes) from the main method.
Final Modifier
As well as being applied to variables (to turn them into constants), the final modifier can also be
applied to classes and methods. Where a class is declared final . . . .
eg final class Coins { .....
it cannot be subclassed. A number of classes in the Java API are final, eg the String class cannot
be subclassed.
Where a method is declared final, no subclass can override that method (all methods of a final
class are automatically final).
Abstract Modifier
The abstract modifier can be applied to both classes and methods. The abstract modifier cannot
be used in conjunction with the final modifier.
An abstract class cannot be instantiated, but can be used as the type for variables and parameters.
Abstract classes would normally be inherited by a non-abstract (concrete) subclass which can
then use the protected and public attributes and methods. Abstract classes serve as a template
class for others.
A method can also be declared as abstract, in which case the body of the method is left
unspecified (a semi-colon is placed where the braces and body would otherwise be located). A
class that contains one or more abstract methods must itself be abstract.
eg public abstract class Car {
:
:
:
public abstract int getNumDoors();
:
}
Synchronized Modifier
The synchronized modifier can be applied only to a method (note the American spelling). A
synchronized method can be accessed by only one thread at a time.
Native Modifier (not in SCJP exam)
The native modifier can be applied only to a method. It indicates that the method is implemented
in platform-dependent code such as C or assembly language. Native methods are automatically
final.
Strictfp Modifier (not in SCJP exam)
The strictfp modifier can be applied to classes and methods.
In a strictfp class all method code in the class will conform to the IEEE754 standard rules for
floating-point calculations. In a class that’s not marked strictfp, individual methods can be so
marked if they conform to IEEE754.
Transient Modifier
The transient modifier can be applied only to instance variables. A transient instance variable is
skipped when its object is serialized. Serialization is the process of sending an objects state (ie
the values of its instance variables) to a stream (and hence to a file or network connection).
Volatile Modifier (not in SCJP exam)
The volatile modifier can be applied only to instance variables. It requires that a thread accessing
the variable must always reconcile its own private copy of the variable with the master copy in
memory. The same functionality can normally be achieved with less effort through the use of
synchronization.