JAVA VARIABLES

Declaring multiple Java variables

When several variables share a data type, Java lets you declare them in one statement. Names and initial values are separated by commas, and the entire declaration ends with one semicolon.

Separate declarations

You can declare each variable on a separate line:

int jonasTaskai = 5;
int onaTaskai = 6;
int rutaTaskai = 50;

System.out.println(jonasTaskai + onaTaskai + rutaTaskai);
Console output

61

  1. The first line creates a number box named jonasTaskaiand puts the number inside it: 5.
  2. On the second line, onaTaskai receives 6, and on the third line, rutaTaskai receives 50.
  3. On the last line, the + operators add the contents of all three boxes: 5 + 6 + 50 = 61.
  4. The method println() displays the calculated number 61 in the console.

Declaring on one line

The same variables of the same type can be written more briefly:

int jonasTaskai = 5, onaTaskai = 6, rutaTaskai = 50;

System.out.println(jonasTaskai + onaTaskai + rutaTaskai);
Console output

61

  1. Java sees the type int and knows that all variables listed on this line will store integers.
  2. Commas separate three different boxes: Jonas’s, Ona’s and Rūta’s points.
  3. Although the declaration takes only one line, each variable still has its own value.
  4. println() adds 5 + 6 + 50, so the console again displays 61.
  5. One semicolon ends the entire combined declaration.
One declaration shares one type.

Only variables of the same type can be combined this way. int and String variables would need separate declarations.

Declaring without initial values

You can first declare several variables and assign different values to them later:

int jonasAmzius, onaAmzius, rutaAmzius;

jonasAmzius = 10;
onaAmzius = 11;
rutaAmzius = 12;

System.out.println(jonasAmzius + ", " + onaAmzius + ", " + rutaAmzius);
Console output

10, 11, 12

  1. The first line creates three int boxes, but they do not yet hold the ages we want to assign.
  2. The next three lines assign each person an age: Jonas 10, Ona 11, Rūta 12.
  3. On the last line, the numbers are joined with the text ", ". This time, the comma and space are simply characters displayed in the console.
  4. Java therefore displays all three values in a clear sequence: 10, 11, 12.
Put a value in first, then use it.

must receive a value before being passed to the method println() or used in a calculation.

One value for multiple variables

You can assign the same value to several variables of compatible types in one chain:

int jonasStartas, onaStartas, rutaStartas;
jonasStartas = onaStartas = rutaStartas = 50;

System.out.println(jonasStartas + onaStartas + rutaStartas);
Console output

150

  1. The first line prepares three boxes for the starting points of Jonas, Ona and Rūta.
  2. Java processes an assignment chain from right to left: first, 50 receives rutaStartas.
  3. The same value is passed to onaStartas, then to jonasStartas. Each now has 50 points.
  4. On the last line, Java calculates 50 + 50 + 50 = 150.
  5. println() displays the total 150.

Shorter code or clearer code?

Declaring multiple variables on one line is concise, but it is not always easiest to read. Choose based on how the variables are related and how long the declaration is.

  • One line works well for a few closely related variables with short names.
  • Use separate lines when names or initial expressions are long.
  • Separate declarations make it easier to add a comment to a particular variable.
  • What matters most is consistent code that the team can easily understand.

Next, learn where declared variables are accessible and when they go out of scope: Java local variables.