Java Building Blocks: Variables & Fields

Boolean, numeric, character, and String variables & fields.

Overview

Java is a strongly- and statically-typed language:

Notes on examples

  1. When typing (or copying and pasting) any of the examples on this page into JShell, the comment portion—starting with the two slashes (//) and continuing to the end of the line—may be omitted.

  2. Some of the literal values in the examples are rendered in different colors; these simply reflect the code syntax highlighting library’s understanding (or, in some cases, misunderstanding) of proper Java syntax. When these examples are typed or pasted into JShell, the colors seen on this page will not be displayed by JShell.

Scope

Variables may be declared at various levels, listed here from the lowest to highest level:

Declaration

Field, variables, and parameters (which we can think of as local variables in a method or constructor, which are assigned a value on invocation) are always declared with the type, followed by the variable name. For example, a variable named running, of the boolean type, would be declared as

boolean running;

We can also assign a value along with the declaration (a declaration-with-assignment statement):

boolean running = false;

Fields (variables declared as members of a class, enum, or interface) typically have modifiers that precede the type in the declaration.1

There are very few constraints—but several important conventions—on how we name variables. First, let’s look at the rules enforced by the compiler:

Now to the conventions. These are long-established and widely accepted conventions, originating in common C practice, formalized in Sun’s (now Oracle’s) “Code Conventions for the Java™ Programming Language, and further codified in the Google Java Style Guide and many other organizations’ style guides.

At DDC, we add a few more rules for bootcamp students, based on the instructors’ experience in developing and maintaining software:

Examples

boolean running; // Conforms with compiler requirements, and with Sun/Oracle and Google 
                 // conventions.
boolean running = 0; // Conforms with compiler variable naming requirements, but DOES NOT
                     // compile, because we are assigning a non-boolean value to a boolean 
                     // variable.
boolean running = false; // Conforms with requirements & conventions, and compiles successfully.
String headline; // Conforms with compiler requirements, and with Sun/Oracle and Google 
                 // conventions.
String _headline; // Conforms with compiler requirements, BUT NOT with Sun/Oracle and Google
                  // conventions.
String $headline; // Conforms with compiler requirements, BUT NOT with Sun/Oracle and Google 
                  // conventions.
String headline = "User Stories"; // Conforms with requirements & conventions, and compiles 
                                  // successfully.
String headline = 'U'; // Conforms with naming requirements & conventions, but DOES NOT compile,
                       // because the literal value is a char.
String headline = 100; // Conforms with naming requirements & conventions, but DOES NOT compile,
                       // because the literal value is an int.
int height; // Conforms with compiler requirements, and with Sun/Oracle and Google conventions.
int height = 168; // Conforms with requirements & conventions, and compiles successfully.
int height = 168.0; // Conforms with naming requirements & conventions, but DOES NOT compile, 
                    // because the literal value is a double.
int height = 168f; // Conforms with naming requirements & conventions, but DOES NOT compile, 
                   // because the f type suffix makes the literal value a float.
int height = "168"; // Conforms to the naming requirements & conventions, but DOES NOT compile, 
                    // because the literal value is a String.
  1. For more information, see “Classes in Java”