Variables

Java variables are fundamental elements in Java programming used to store data that can be manipulated and accessed throughout a program. They act as containers that hold values which can be changed during the execution of the program. Understanding variables is essential for any Java programmer as they are integral to writing dynamic and flexible code.

 

Variable Declaration

In Java, variables must be declared before they can be used. Declaring a variable involves specifying its data type followed by the variable name. This tells the Java compiler what type of data the variable will hold and reserves a memory location for it.

Example:

Declaration
int number;

 

Variable Initialization

Initialization means assigning a value to a variable at the time of declaration or later in the code. A variable can be initialized when it is declared, or you can assign a value to it later.

Examples:

Initialization
number = 10;
Combined Declaration and Initialization
int number = 10;

 

Types of Variables

Java variables can be classified based on their scope, lifetime, and how they are stored.

Local Variables

  • Scope: Limited to the block of code or method where they are declared.
  • Lifetime: Exist only during the execution of that block of code or method.
  • Initialization: Must be initialized before use.

Example:

Local Variables
public void myMethod() {
    int localVar = 5;
}

Instance Variables (Non-Static Fields)

  • Scope: Belong to an instance of a class (i.e., an object).
  • Lifetime: Exist as long as the object exists.
  • Initialization: Can have default values if not explicitly initialized (e.g., 0 for int, null for objects).
Instance Variables
public class MyClass {
    int instanceVar; // Instance variable
    public void method() {
        // Code using instanceVar
    }
}

Static Variables (Static Fields)

  • Scope: Belong to the class itself rather than any instance.
  • Lifetime: Exist for the duration of the program.
  • Initialization: Can be initialized at the point of declaration or in a static block.

Examples:

Static Variable
public class MyClass {
    static int classVar = 10;
}
Static Variable with Initialization in a static block
public class MyClass {
    static int classVar;

    static {
    	classVar = 10;
    }
}

 

Variable Naming

Variable names in Java should follow certain rules:

  • Variable names are case-sensitive.
  • A variable’s name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits.
  • White space is not permitted in variable names.

Starting Character

  • The name must begin with a letter, the dollar sign $, or the underscore character _.
  • The underscore character _ can technically be used at the beginning of a variable’s name, but this practice is discouraged.

Subsequent Characters

  • Subsequent characters in a variable name may be letters, digits, dollar signs, or underscore characters.

Conventions & Common Sense

  • The dollar sign character $ is conventionally never used (auto-generated names may sometimes contain the dollar sign, but it should be avoided in user-defined variable names.) in variable names.w
  • Conventionally, variable names should start with a letter, not $ or _.
  • Use full words for variable names instead of cryptic abbreviations.
  • Using full words makes your code easier to read and understand.
  • Full words can make your code self-documenting.
  • Examples of intuitive variable names include cadence, speed, and gear rather than abbreviated versions like s, c, and g.
  • Variable names must not be keywords or reserved words.
Last updated on