Java Notes
A variable is a named place where a Java program keeps a value, such as a learner
A variable is a named place for a value. Instead of writing the number 85 everywhere in a program, you can store it in a variable named score. Later, Java can read score, print it, compare it, or replace it with a new score.
Think of a variable like a labelled box:
- the label is the variable name, such as
score; - the kind of item allowed is the data type, such as
intfor whole numbers; - the item inside is the current value, such as
85.
Create, give a value, and change a variable
There are three common actions when working with a variable.
- Declare it: tell Java the type and name of the box.
- Give it a value: put the first item in the box.
- Change the value: replace the old item with a new one.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Read the important lines carefully:
int completedLessons;creates a whole-number variable but does not give it a value yet.completedLessons = 4;puts4into the variable for the first time.completedLessons = completedLessons + 1;reads the current value (4), adds1, then stores the new value (5) back in the same variable.String learnerName = "Asha";creates a text variable and gives it a value in one line.
Java must know a local variable's first value before you use it. This is not allowed because Java does not know what score contains:
// int score;
// System.out.println(score);Fix it by giving the variable a value first: int score = 0;.
A variable keeps the latest value
The variable name does not permanently mean one value. It points to the current value stored under that name.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
When the second assignment runs, score no longer holds 60; it holds 75. This is useful for totals, counters, marks, shopping-cart quantities, and any value that changes while the program works.
Where can a variable be used?
The place where you create a variable decides where its name can be used. Java calls this area the variable's scope. A simple rule is: a variable exists from the line where you create it until the closing curly bracket of that block.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
total is created inside main, before the loop, so both the loop and the final print line can use it. number is created inside the for loop, so it exists only while that loop is running. After the closing }, Java removes the name number from this part of the program.
Create a variable as close as possible to where you need it. This keeps code easier to read and prevents another part of the program from changing it by mistake.
Three places variables can live
Until now, all variables were inside main. These are called local variables. In classes that create objects, you also see instance variables and static variables.
Imagine a learning app with many students:
- Every student needs their own name. Store this in an instance variable.
- The app needs one total count of all students. Store this in a static variable.
- A score used only while printing a report is temporary. Store this in a local variable.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Read the example in this order:
String namebelongs to one Student. Asha has her ownname, and Ravi has his ownname.static int studentCountbelongs to the complete Student class. Both students share one count.- Every time
new Student(...)creates a student, the constructor adds one to the shared count. int score = 82is local toprintReport. It exists only while that method is printing one report.
| Variable type | Where you create it | Who can use its value? | Example |
|---|---|---|---|
| Local variable | Inside a method or loop | Only code inside that method/block | int score = 82; |
| Instance variable | Inside a class, outside methods | One object and its methods | String name; |
| Static variable | Inside a class, with static | All objects of that class share one value | static int studentCount; |
You will use local variables most often at first. Instance and static variables become easier after you practise classes and objects.
Make a value fixed with final
Sometimes a value should not change after you set it. Add final before the type to protect it from another assignment.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
PASS_MARK is a value you want to keep fixed. final means you can give it a value once, but cannot assign a new value to that variable later. The usual Java style for a fixed value is capital letters with underscores: PASS_MARK, MAX_ATTEMPTS.
A note about var
You may see var in modern Java examples:
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Java looks at "Java Fundamentals" and understands that courseName is a String. var does not mean the type can change later; Java still fixes the type as String. While learning, write String courseName when it helps you see the type clearly. Use var only when the value makes the type obvious.
Try it yourself
Create a program named ReadingTracker.
- Make a
Stringvariable namedbookTitle. - Make an
intvariable namedpagesReadand start it at10. - Add
5topagesRead. - Print the title and the updated page count.
Hint
Use the same pattern shown earlier: pagesRead = pagesRead + 5;. Java reads the old value, adds five, then stores the result back in pagesRead.
One working answer
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Change the title and starting page count, then run it again. The most important thing to observe is that pagesRead prints the new total, not the original 10.
Continue the lesson path