Limitations of var
Where var cannot be used.
Limitations of var is a free Java Academy lesson on CoddyKit — lesson 4 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.
var Has Rules
var is convenient but limited. It works only for local variables with an initializer. Knowing where it cannot be used saves frustrating compile errors.
Locals Only
var is allowed only for local variables (inside methods, loops, etc.). The example below is valid because the variable is local.
public class Main {
public static void main(String[] args) {
var greeting = "OK for locals";
System.out.println(greeting);
}
}Not for Fields
You cannot use var for class fields. Fields must declare an explicit type. Here count is a normal field.
public class Main {
static int count = 0;
public static void main(String[] args) {
count = 5;
System.out.println(count);
}
}Not for Method Parameters
Method parameters need explicit types. var is not allowed in a parameter list, so this method declares String name directly.
public class Main {
static void greet(String name) {
System.out.println("Hi " + name);
}
public static void main(String[] args) {
greet("Lee");
}
}Not for Return Types
A method's return type cannot be var; it must be explicit. The compiler does not infer return types.
public class Main {
static int square(int x) {
return x * x;
}
public static void main(String[] args) {
System.out.println(square(6));
}
}Must Have an Initializer
You cannot write var x; with no value. Without an initializer the compiler has nothing to infer from. Always assign immediately.
public class Main {
public static void main(String[] args) {
var ready = true;
System.out.println(ready);
}
}No null Initializer
var x = null; fails because null has no specific type. Give a typed value, like an empty string, instead.
public class Main {
public static void main(String[] args) {
var text = "";
text = "now has a value";
System.out.println(text);
}
}No Array Initializer Shorthand
You cannot write var nums = {1, 2, 3};. The brace shorthand needs a declared array type. Use new int[]{...} instead.
public class Main {
public static void main(String[] args) {
var nums = new int[]{1, 2, 3};
System.out.println(nums.length);
}
}Not a Reserved Word
Interestingly, var is a reserved type name, not a keyword. You still cannot name a class var, but the rule is narrow. In practice, treat it like any modern syntax.
public class Main {
public static void main(String[] args) {
var var2 = "identifiers can contain var";
System.out.println(var2);
}
}No Lambda Without Target Type
You cannot assign a bare lambda to var, because a lambda needs a target functional interface type to infer from. Declare the interface type explicitly.
import java.util.function.IntUnaryOperator;
public class Main {
public static void main(String[] args) {
IntUnaryOperator doubler = x -> x * 2;
System.out.println(doubler.applyAsInt(21));
}
}Summary of Restrictions
Remember the boundaries: locals only, must initialize, no null, no bare lambda, no array brace shorthand, and not for fields, parameters, or return types.
public class Main {
public static void main(String[] args) {
var ok = 42;
System.out.println("var is for locals: " + ok);
}
}Quick Check
Which of the following is a valid use of var?
Recap
You learned the limitations of var:
- Allowed only for local variables with an initializer
- Not for fields, parameters, or return types
- Cannot infer from
nullor a bare lambda - No array brace shorthand
- It is a reserved type name, not a keyword
Frequently asked questions
Is the “Limitations of var” lesson free?
Yes — the full text of “Limitations of var” 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 “Limitations of var”?
Where var cannot be used. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Limitations of var” 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