Section A.8. Fields


[Page 549]

A.8. Fields

Object fields are the data or state that each object of a class will have. Class (static) fields are in the object that defines the class so there is only one and all objects of the class have access to it. Fields are defined inside of a class definition. The convention is to start field names with a lowercase letter and uppercase the first letter of each additional word.

To declare an object field, use:

visibility type name;


To declare a field and give it a value, use:

visibility type name = expression;


The visibility for fields is usually private so that an object can protect its data from being directly accessed by code in other classes.

To declare a class field, use:

visibility static type name;


To declare a constant field, use:

public static final type name;




Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
ISBN: N/A
EAN: N/A
Year: 2007
Pages: 191

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net