Using var for Locals
Let the compiler infer the type.
Using var for Locals is a free Java Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Local Type Inference
Since Java 10, you can declare local variables with var and let the compiler infer the type from the initializer. This is local type inference. The variable is still strongly typed.
Your First var
Replace an explicit type with var when assigning a value. The compiler sees the literal and infers the right type.
public class Main {
public static void main(String[] args) {
var message = "Hello";
var count = 42;
System.out.println(message + " " + count);
}
}var Is Not Dynamic Typing
var does not make Java dynamically typed. Once inferred, the type is fixed. Assigning a value of a different type later is a compile error.
public class Main {
public static void main(String[] args) {
var n = 10;
n = 20;
System.out.println(n);
}
}Inferring Object Types
var works for objects too. The inferred type is the type returned by the initializer expression.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
var list = new ArrayList<String>();
list.add("a");
list.add("b");
System.out.println(list);
}
}Less Repetition
Long generic types no longer need to be written twice. var removes the redundant left-hand type.
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
var scores = new HashMap<String, Integer>();
scores.put("Alice", 90);
System.out.println(scores);
}
}Inferring From Method Returns
When a method returns a value, var picks up its declared return type automatically.
public class Main {
static String greet() {
return "Hi there";
}
public static void main(String[] args) {
var g = greet();
System.out.println(g.toUpperCase());
}
}var With Primitives
An integer literal infers int, a decimal infers double. Be aware of which primitive you get.
public class Main {
public static void main(String[] args) {
var i = 5;
var d = 5.0;
var b = true;
System.out.println(i + " " + d + " " + b);
}
}Still Compile-Time Checked
Because the type is known at compile time, the compiler still catches type errors. Calling a String method on an inferred int would fail to compile.
public class Main {
public static void main(String[] args) {
var name = "Coddy";
System.out.println(name.length());
}
}Must Initialize Immediately
A var declaration requires an initializer on the same line. The compiler needs a value to infer the type from.
public class Main {
public static void main(String[] args) {
var total = 0;
for (int i = 1; i <= 5; i++) {
total += i;
}
System.out.println(total);
}
}Works With Arrays
You can infer an array type, as long as the initializer makes the element type clear.
public class Main {
public static void main(String[] args) {
var numbers = new int[]{1, 2, 3, 4};
var sum = 0;
for (var n : numbers) {
sum += n;
}
System.out.println(sum);
}
}A Practical Example
Combining var in a small program keeps the code clean while staying fully typed.
import java.util.List;
public class Main {
public static void main(String[] args) {
var fruits = List.of("apple", "banana", "cherry");
for (var fruit : fruits) {
System.out.println(fruit.toUpperCase());
}
}
}Quick Check
What best describes how var works in Java?
Recap
You learned to use var for locals:
- The compiler infers the type from the initializer
- The variable is still strongly, statically typed
- An initializer is required on the same line
- It reduces repetition with long generic types
- Works with primitives, objects, and arrays
Frequently asked questions
Is the “Using var for Locals” lesson free?
Yes — the full text of “Using var for Locals” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Using var for Locals”?
Let the compiler infer the type. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Using var for Locals” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Using var for Locals
- When var Helps Readability
- var in for Loops
- Limitations of var