Java Declare Multiple Variables - Syntax and Examples

Learn how to declare multiple variables of the same type in Java using a comma-separated list. This method streamlines code and improves readability by grouping related variables together.



Java Declare Multiple Variables

To declare more than one variable of the same type, use a comma-separated list:

Example:

Instead of writing:

int x = 5;
int y = 6;
int z = 50;
System.out.println(x + y + z);

You can simply write:

int x = 5, y = 6, z = 50;
System.out.println(x + y + z);
Syntax

int x = 5, y = 6, z = 50;
System.out.println(x + y + z);
Output

61

You can also assign the same value to multiple variables in one line:

Example:

int x, y, z;
x = y = z = 50;
System.out.println(x + y + z);
Syntax

int x, y, z;
x = y = z = 50;
System.out.println(x + y + z);
Output

150