May 2003
Intermediate to advanced
384 pages
9h 37m
English
The Java language provides four access modes, which should be used properly: public, protected, private, and package private, which is the default if no mode is explicitly specified. A common example of improper use is the inexperienced programmer who, when writing a time zone class, mistakenly declares fields or variables that are publicly accessible:
public TimeZone defaultZone;
This design has a number of problems. First, any person or code, including untrusted code, can access this field and directly change the value of the default time zone. Second, because multiple threads can access this field, some synchronization is needed. Following is a better design:
private TimeZone defaultZone = null; public synchronized ...
Read now
Unlock full access