Java Reference
In-Depth Information
This approach promotes the principle of reuse , that is, the practice of writing a
class (a piece of code) that will be used by other classes. Consumer classes only have
the view(s) of the class that the class designer thinks appropriate. Consumer classes
then use these classes as if they were simply another type of variable. As I have
demonstrated with the COBOL programs, it is certainly possible to write programs
that do this in any programming language; but with Java, it is almost impossible to
code in any other way.
Up to this point, I've introduced some OO concepts and described how Java
supports these concepts. So it's a good time to present a more complete definition
of these Java terms and syntax.
J AVA D ATA M EMBERS
Data members are variables in the normal understanding of variables in computer
languages (for example, data ITEMS in COBOL). They are members of a class in the
sense that they are attributes of that class and contain its current state information.
Data members can be qualified with access controls, which define how visible
the data members are to other classes. Data members can be either public, private,
or package variables. Public members are visible outside the class, and they can be
set or evaluated by other classes. Private members are only visible inside the class;
other classes cannot view or modify these variables. Package members are visible to
all classes in a package (you can think about a package as a sort of directory of re-
lated class files). Package is the default access condition.
public int msgSize;
private int counter = 0;
char interfaceInUse;
Java also defines another data member access control, protected , which is used
to support inheritance. I will discuss protected access later, along with inheritance.
All data members are normally associated with an instance of a class (i.e., an
object). As I discussed earlier, each new instance of a class contains unique copies
of all of its variables, both public and private.
With any rule, there is an exception. (Don't you hate it when you think you fi-
nally understand something, and then the instructor throws a curve ball?) Class
variables are data members that are associated with all instances of a class. These
variables persist for the duration of the program and are shared by all instances of
this class. In many respects, class variables are similar to items defined in a COBOL
Search WWH ::




Custom Search