Aral Code

Basics Lesson 4 of 21

Java Variables

Declare and initialize variables with a type, change their values, use final for constants, follow naming conventions, and use var for local type inference.

A variable stores a value your program can use and change. In Java, every variable has a type that's fixed when you create it: a variable made for whole numbers can never hold text. This strictness catches many mistakes before the program even runs.

Declaring a variable

type name = value;
public class Main {
    public static void main(String[] args) {
        String name = "Juan Dela Cruz";
        int gradeLevel = 11;
        double average = 89.75;
        boolean isOfficer = true;
        char section = 'A';

        System.out.println(name + " (Grade " + gradeLevel + "-" + section + ")");
        System.out.println("Average: " + average);
        System.out.println("Officer? " + isOfficer);
    }
}

Output

Juan Dela Cruz (Grade 11-A)
Average: 89.75
Officer? true
  • String holds text, in double quotes.
  • int holds whole numbers.
  • double holds decimals.
  • boolean holds true or false.
  • char holds a single character, in single quotes.

Declaring first, assigning later

int score;          // declared
score = 95;         // assigned (initialized)

Changing values

public class Main {
    public static void main(String[] args) {
        int baon = 150;
        baon = baon - 45;    // lunch
        baon -= 20;          // fare
        baon += 50;          // extra from Lola
        System.out.println("Baon left: " + baon);

        int count = 0;
        count++;             // add 1
        count++;
        System.out.println("Count: " + count);
    }
}

Output

Baon left: 135
Count: 2

Type safety

Once declared, a variable keeps its type:

int age = 16;
age = "sixteen";     // error!

Error

Main.java:4: error: incompatible types: String cannot be converted to int

Constants with final

final makes a variable unchangeable. By convention, constants are written in UPPER_SNAKE_CASE:

public class Main {
    public static void main(String[] args) {
        final int PASSING_GRADE = 75;
        final double VAT = 0.12;

        double price = 250;
        System.out.println("With VAT: " + (price + price * VAT));
        System.out.println("Passing grade: " + PASSING_GRADE);
        // PASSING_GRADE = 60;  // error: cannot assign a value to final variable
    }
}

Output

With VAT: 280.0
Passing grade: 75

Naming rules and conventions

  • Names may contain letters, digits, _ and $, but can't start with a digit.
  • Names are case-sensitive: score and Score are different.
  • You can't use Java keywords like int, class or public.
  • Variables use camelCase: finalGrade, studentCount.
  • Classes use PascalCase: Main, Student, BankAccount.
  • Constants use UPPER_SNAKE_CASE: MAX_STUDENTS.

Several variables in one line

int math = 88, science = 91, english = 85;
int total = math + science + english;

var: let Java figure out the type

Since Java 10, you can write var for local variables, and Java infers the type from the value:

var school = "Rizal NHS";   // String
var students = 1240;        // int
var rate = 0.85;            // double

The variable still has a fixed type; var just saves typing. Many teachers prefer you write the type explicitly while learning, so you always know what's inside.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Which type stores 89.5?
2. Which quotes does a char use?
3. What does final do?
4. Which follows Java's convention for a variable name?

By Sir Reyel · Last updated 2026-12-04