Variables Lesson

Welcome to the building blocks of memory.

Variables are one of the most fundamental concepts in programming. Think of them as labeled containers that allow your programs to store, manage, and manipulate information on the fly.

What is a Variable? The "Box" Analogy

At its core, a variable is a named storage location in your computer's memory that holds a piece of information. This information, or "value," can be used and changed as your program runs. Let's break down the analogy.

πŸ“¦ The Box

This represents the variable itselfβ€”a container in memory reserved for storing data.

🏷️ The Label

This is the variable's name. It's a unique identifier you choose so you know what's inside the box.

πŸ’Ž The Contents

This is the variable's valueβ€”the actual piece of information you store inside the box.

Key Idea

You can change what's inside the box (the value), but the label on the box (the name) stays the same once it's created.

Declaring a Variable in Java

Before you can use a variable, you must "declare" it. This tells the computer you want to create a new storage container. In Java, a statically-typed language, you must also specify what *type* of data the container will hold.

dataType variableName = value;

// Example declarations in Java:
int robotScore = 150;
double motorSpeed = 0.75;
boolean isRedAlliance = true;

Naming Conventions

Choosing clear, consistent names is crucial for writing readable code. While there are strict rules (like no spaces), there are also style conventions that make your code professional. The most common styles are:

camelCase

The first word is lowercase, and the first letter of each subsequent word is capitalized. This is the standard convention for variables and methods in Java.

int playerScore;
double motorSpeed;
String teamName;

PascalCase

The first letter of every word is capitalized. In Java, this convention is used almost exclusively for naming classes.

public class RobotController { ... }
public class SwerveModule { ... }

snake_case

All words are lowercase and separated by underscores. While you won't see this often in Java, it's the standard convention in other languages like Python and is sometimes used for constants in Java.

final int MAX_SPEED = 3;

Variable Scope: Where Does a Variable Live?

Scope defines the region of your code where a variable can be accessed. A variable declared inside one method isn't accessible from another. Understanding scope is key to organizing your code and avoiding errors.

Local Variables

Declared inside a method or code block (like a loop). They only exist and can only be used within that block.

public void calculateScore() {
    int bonusPoints = 50; // 'bonusPoints' is a local variable
    // It can only be used inside this method.
    int totalScore = baseScore + bonusPoints;
}

Instance Variables (Fields)

Declared inside a class but outside any method. Each object (instance) of the class gets its own personal copy of this variable.

public class Robot {
    public String robotName; // An instance variable
    public double weight;    // Another instance variable
    
    public void displayStats() {
        // Both variables are accessible here.
        System.out.println("Name: " + robotName);
    }
}

Static (Class) Variables

Declared with the `static` keyword. There is only one copy of this variable, shared by all objects of the class. It belongs to the class itself.

public class Team {
    public static int teamNumber = 2910; // A static variable
    
    public void printTeamNumber() {
        // All objects of the Team class share this one number.
        System.out.println(Team.teamNumber);
    }
}

Test Your Knowledge

Question: According to Java conventions, which of the following is the best name for a variable that stores the current speed of an arm motor?