1.1 A Beginner’s Guide to Java Variables


22/05/25

4 mins

A Beginner’s Guide to Java Variables: Types, Names, and Best Practices

If you're starting your Java journey, variables are one of the first concepts you'll meet. Variables are essential—they hold the data your program uses while it runs. Let's break down the key ideas from the official Java documentation in simple terms so you can write cleaner, more effective code.

What Are Variables in Java?

Variables are containers that store information. You use them to remember data while your program is running. In Java, the terms "field" and "variable" often come up and sometimes cause confusion for beginners. Here's how they differ:

  • Fields: Variables that belong to a class or an object.
  • Variables (in general): Covers fields, local variables, and parameters.

Java has several types of variables, each with its specific purpose and behavior.

Types of Variables in Java

1. Instance Variables (Non-Static Fields)

  • Definition: Belong to each object you create from a class.
  • Example: If you have two bicycle objects, each has its own currentSpeed variable.
  • Syntax:
    int speed = 0;

2. Class Variables (Static Fields)

  • Definition: Shared by all objects of a class.

  • How to declare: Use the static keyword.

  • Example: If every bicycle made from your class should have 6 gears, that can be a static variable.

  • Syntax:

    static int numGears = 6;

    You can also make it final if you don't want the value to change:

    static final int NUM_GEARS = 6;

3. Local Variables

  • Definition: Exist only inside methods or code blocks. They're used for temporary storage.
  • Scope: Only available within the method where they are declared.
  • Syntax:
    int count = 0;
    No special keyword marks a variable as local—it just depends on where it's declared.

4. Parameters

  • Definition: Input values for methods or constructors.
  • Example:
    public void setSpeed(int speed) { ... }
    Here, speed is a parameter and acts like a local variable inside the method.

Naming Variables in Java

Knowing how to name your variables makes code easy to read and maintain[1].

Rules and Conventions:

  • Names are case-sensitive (Speed and speed are different).
  • Start with a letter, underscore (_), or dollar sign ($), but starting with a letter is best.
  • Can't use spaces.
  • Don't use Java keywords (like class, int, or for).
  • Avoid starting names with $ or _: while allowed, it’s discouraged by convention.
  • Stick to full words instead of abbreviations (speed instead of s).
  • For one-word names: use all lowercase.
  • For multiple words: capitalize every new word after the first (currentSpeed, gearRatio).
  • Constants: Use static final and write in ALL_UPPERCASE_WITH_UNDERSCORES (NUM_GEARS).

Examples:

// Good variable names int speed; int currentGear; int gearRatio; // Constant variable static final int NUM_GEARS = 6;

Final Notes

When writing Java programs, always think about which type of variable fits your need—do you want each object to track its own value, share one value, or just need something temporary inside a method?
Also, follow naming conventions for clear and professional code—your future self and teammates will thank you!

If you want more detail, check out the full official guide on Java variables at dev.java.

Happy coding!


PREVIOUS: 1.0 Basics of OOP with java

NEXT: 1.2 Getting Started with Java Primitive Types