Initialize Multiple Variables in Java

Rupam Yadav Dec 10, 2020 Nov 27, 2020
  1. Initialize Multiple String Variables With the Same Value in Java
  2. Initialize Multiple Objects With the Same Class in Java
Initialize Multiple Variables in Java

In this article, we will go through the steps to follow when we want to initialize multiple variables with the same value in Java. We will discuss why we can’t initialize all the variables with the same value during the declaration.

Initialize Multiple String Variables With the Same Value in Java

In the below example 1, we declare the variables one, two, and three of the String type and then we initialize all the three variables with the same value. We are doing this by chained assignment. it means that we assign the value of the leftmost variable to all the variables present on the right of the assignment operator.

  • Example 1:
package com.company;

public class Main {

    public static void main(String[] args) {
        String one, two, three;
        one = two = three = "All three variables will be initialized with this string";

        System.out.println(one);
        System.out.println(two);
        System.out.println(three);
    }
}

Output:

All three variables will be initialized with this string
All three variables will be initialized with this string
All three variables will be initialized with this string

If our requirement is to initialize the variables during declaration then we can use the chained assignment as we have done below. But it decreases the readability of the program as there might be more than one developer working on the same project.

  • Example 2:
package com.company;

public class Main {

    public static void main(String[] args) {
        String one, two, three = two = one = "All three variables will be initialized with this string";

        System.out.println(one);
        System.out.println(two);
        System.out.println(three);
    }
}

Output:

All three variables will be initialized with this string
All three variables will be initialized with this string
All three variables will be initialized with this string

Initialize Multiple Objects With the Same Class in Java

We saw that we can store the same value in all the three String variables using the chained assignment technique. But can we do the same when we want to save the reference of the same class object in multiple variables? Let’s see.

When we initialize a variable with a class constructor using the new keyword, that variable is called an object and it points to the class. We can create multiple objects with the same class using chained assignment but it will point to the same reference which means that if we change the value of firstObj the secondObj will also reflect the same changes.

We can check the following example in which the three objects - firstObj, secondObj, and thirdObj are assigned together, but fourthObj is assigned separately. The output shows the difference.

package com.company;

public class Main {

    public static void main(String[] args) {

        SecondClass firstObj, secondObj, thirdObj;
        firstObj = secondObj = thirdObj = new SecondClass();

        firstObj.aVar = "First Object";

        secondObj.aVar = "Second Object";

        SecondClass fourthObj = new SecondClass();
        fourthObj.aVar = "Fourth Object";

        System.out.println(firstObj.aVar);
        System.out.println(secondObj.aVar);
        System.out.println(thirdObj.aVar);

        System.out.println(fourthObj.aVar);

    }

}

class SecondClass{
    String aVar;
}

Output:

Second Object
Second Object
Second Object
Fourth Object
Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn

Related Article - Java Variable