THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

2.1.2. Class Modifiers


A class declaration can be preceded by class modifiers that give the class certain properties:



  • annotations Annotations and annotation types are discussed in Chapter 15.
    public A public class is publicly accessible: Anyone can declare references to objects of the class
    or access its public members. Without a modifier a class is only accessible within its own package.
    You'll learn about general access control in Section 2.3 on page 47. Packages and related accessibility
    issues of classes and members are discussed in Chapter 18.



abstract An abstract class is considered incomplete and no instances of the class may be created.
Usually this is because the class contains abstract methods that must be implemented by a
subclass. You'll learn about this in "Abstract Classes and Methods" on page 97.



  • final A final class cannot be subclassed. Subclassing is discussed in Chapter 3.
    strict floating point A class declared strictfp has all floating-point arithmetic in the class
    evaluated strictly. See "Strict and Non-Strict Floating-Point Arithmetic" on page 203 for details.



A class cannot be both final and abstract.


A class declaration can be preceded by several modifiers. Modifiers are allowed in any order, but we
recommend that you adopt a consistent order to improve the readability of your code. We always use, and
recommend, the order listed.


While we won't be concerned about class modifiers in this chapter you need to know a little about public
classes. Most Java development tools require that a public class be declared in a file with the same name as
the class, which means there can only be one public class declared per file.


Exercise 2.1: Write a simple Vehicle class that has fields for (at least) current speed, current direction in
degrees, and owner name.


Exercise 2.2: Write a LinkedList class that has a field of type Object and a reference to the next
LinkedList element in the list.


2.2. Fields


A class's variables are called fields; the Body class's name and orbits variables are examples. A field
declaration consists of a type name followed by the field name and optionally an initialization clause to give
the field an initial value. Every Body object has its own specific instances of three fields: a long that
uniquely identifies the body from all others, a String that is its name, and a reference to another Body
around which it orbits. Giving each separate object a different instance of the fields means that each object has
its own unique statesuch fields are known as instance variables. Changing the orbits field in one Body
object does not affect the orbits field in any other Body object.


Field declarations can also be preceded by modifiers that control certain properties of the field:



  • annotations Annotations and annotation types are discussed in Chapter 15.

  • access modifiers These are discussed in Section 2.3 on page 47.

  • static This is discussed below.

  • final This is discussed below.

  • transient This relates to object serialization and is discussed in Section 20.8 on page 549.
    volatile This relates to synchronization and memory model issues and is discussed in Section 14.10 on
    page 370.


Free download pdf