Database Reference
In-Depth Information
MyApp2 m = new MyApp2();
MyRef useRef = new MyRef();
m.setRef( useRef );
ARRAY mA = m.getArray();
myRef = new MyRef();
}
Notice one last thing in the main() method definition. It sets the value of the static myRef member to
an instance of MyRef . From a static method, only static members of the class can be referenced. If we
tried to set the value of myArray from within main() , we would get an error at compile time, because until
we create an instance of MyApp2 , there is no myArray member—it is not static .
Public and Private Modifiers
public and private modifiers can exist on both methods and member variables. These help delineate
the items in a class that the user of a class has direct access to, from those items that the user of a class
cannot see (directly). These are scope modifiers. That is the important thing about public and private : it
is important because it has some implications for security that we will discuss later.
In Listing 3-3, the myArray member is private, so someone using this class cannot get or modify
myArray directly. However, we have provided the method getArray() which is public and can return
myArray to anyone.
private ARRAY myArray = null;
static MyRef myRef;
public ARRAY getArray() {
return myArray;
}
void setRef( MyRef useRef ) {
myRef = useRef;
}
By default, methods and members, like the myRef member and the setRef() method, which are not
declared as private or public , are generally only accessible to other classes that come from the same
package. One additional scope modifier is protected . Protected scope is like the default, except it allows
you to permit subclasses of your class that are defined outside your package to see your members and
methods.
One design pattern (approach) declares all the class member variables are private , then sets about
establishing getters and setters (public methods that provide a way for other objects to get or set the
private members) to read from and write to the members. This is a feature of JavaBeans, and often of
Enterprise Java Beans, and lends to their usefulness as components in an IDE. They can be distributed
and incorporated into an IDE or application server without prior knowledge or definition by virtue of
their getters and setters—by reflection (or XML definition), the IDE can list the public getter and setter
methods of a bean and provide access to the private members.
We have provided a getter method in our example code for the myArray member: getArray() . We
have also provided a setter method for the myRef member: setRef() ; although, our method names don't
conform to JavaBeans standards, because we are not writing JavaBeans, and we needn't implement all
that boilerplate code.
 
Search WWH ::




Custom Search