As you learned in the previous lesson, an object stores its state in fields.
int cadence = 0; int speed = 0; int gear = 1;
The
What Is an Object? discussion introduced you to fields, but you probably have still a few questions, such as: What are the rules and conventions for naming a field? Besides int
, what other data types are there? Do fields have to be initialized when they are declared? Are fields assigned a default value if they are not explicitly initialized? We'll explore the answers to such questions in this lesson, but before we do, there are a few technical distinctions you must first become aware of. In the Java programming language, the terms "field" and "variable" are both used; this is a common source of confusion among new developers, since both often seem to refer to the same thing.
The Java programming language defines the following kinds of variables:
static
keyword. Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words); the currentSpeed
of one bicycle is independent from the currentSpeed
of another.static
modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. A field defining the number of gears for a particular kind of bicycle could be marked as static
since conceptually the same number of gears will apply to all instances. The code static int numGears = 6;
would create such a static field. Additionally, the keyword final
could be added to indicate that the number of gears will never change.int count = 0;
). There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared which is between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class.Bicycle
class and in the main
method of the "Hello World!" application. Recall that the signature for the main
method is public static void main(String[] args)
. Here, the args
variable is the parameter to this method. The important thing to remember is that parameters are always classified as "variables" not "fields". This applies to other parameter-accepting constructs as well (such as constructors and exception handlers) that you'll learn about later in the tutorial.Having said that, the remainder of this tutorial uses the following general guidelines when discussing fields and variables. If we are talking about "fields in general" (excluding local variables and parameters), we may simply say "fields". If the discussion applies to "all of the above", we may simply say "variables". If the context calls for a distinction, we will use specific terms (static field, local variables, etc.) as appropriate. You may also occasionally see the term "member" used as well. A type's fields, methods, and nested types are collectively called its members.
$
", or the underscore character "_
". The convention, however, is to always begin your variable names with a letter, not "$
" or "_
". Additionally, the dollar sign character, by convention, is never used at all. You may find some situations where auto-generated names will contain the dollar sign, but your variable names should always avoid using it. A similar convention exists for the underscore character; while it's technically legal to begin your variable's name with "_
", this practice is discouraged. White space is not permitted.cadence
, speed
, and gear
, for example, are much more intuitive than abbreviated versions, such as s
, c
, and g
. Also keep in mind that the name you choose must not be a
keyword or reserved word.gearRatio
and currentGear
are prime examples of this convention. If your variable stores a constant value, such as static final int NUM_GEARS = 6
, the convention changes slightly, capitalizing every letter and separating subsequent words with the underscore character. By convention, the underscore character is never used elsewhere.