Initializing multiple variables to the same value in Java

JavaVariablesInitializationDeclaration

Java Problem Overview


I'm looking for a clean and efficient method of declaring multiple variables of the same type and of the same value. Right now I have:

String one = "", two = "", three = "" etc...

But I'm looking for something like:

String one,two,three = ""

Is this something that is possible to do in java? Keeping efficiency in mind.

Java Solutions


Solution 1 - Java

String one, two, three;
one = two = three = "";

This should work with immutable objects. It doesn't make any sense for mutable objects for example:

Person firstPerson, secondPerson, thirdPerson;
firstPerson = secondPerson = thirdPerson = new Person();

All the variables would be pointing to the same instance. Probably what you would need in that case is:

Person firstPerson = new Person();
Person secondPerson = new Person();
Person thirdPerson = new Person();

Or better yet use an array or a Collection.

Solution 2 - Java

You can declare multiple variables, and initialize multiple variables, but not both at the same time:

 String one,two,three;
 one = two = three = "";

However, this kind of thing (especially the multiple assignments) would be frowned upon by most Java developers, who would consider it the opposite of "visually simple".

Solution 3 - Java

No, it's not possible in java.

You can do this way .. But try to avoid it.

String one, two, three;
one = two = three = "";

Solution 4 - Java

Works for primitives and immutable classes like String, Wrapper classes Character, Byte.

int i=0,j=2   
String s1,s2  
s1 = s2 = "java rocks"

For mutable classes

Reference r1 = Reference r2 = Reference r3 = new Object();`  

Three references + one object are created. All references point to the same object and your program will misbehave.

Solution 5 - Java

You can do this:

String one, two, three = two = one = "";

But these will all point to the same instance. It won't cause problems with final variables or primitive types. This way, you can do everything in one line.

Solution 6 - Java

I do not think that is possible you have to set all the values individualling (like the first example you provided.)

The Second example you gave, will only Initialize the last varuable to "" and not the others.

Solution 7 - Java

Edit: As Zeeen pointed out this will not work in Java. The question I'd intended to answer was in Groovy as well, this was submitted in error.


Way too late to this but the simplest way I've found is:

String foo = bar = baz = "hello"
println(foo)
println(bar)
println(baz)

Output:

hello
hello
hello

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser83643View Question on Stackoverflow
Solution 1 - JavaAlfredo OsorioView Answer on Stackoverflow
Solution 2 - JavaMichael BorgwardtView Answer on Stackoverflow
Solution 3 - JavaSaurabh GokhaleView Answer on Stackoverflow
Solution 4 - JavaSrujan Kumar GullaView Answer on Stackoverflow
Solution 5 - Javahyper-neutrinoView Answer on Stackoverflow
Solution 6 - JavaRMTView Answer on Stackoverflow
Solution 7 - Javastarscream_disco_partyView Answer on Stackoverflow