asda asda - 1 month ago 8
Java Question

Declare multiple String variables and initialize them to all to null at once

I want to declare all of them

. Am I doing someting wrong or is this the right method?

String a = null, b = null, c = null, d = null;

(Is there any more compact syntax for doing this?)


Yep. That's the way to do it.

You could also do

String a, b, c, d;
a = b = c = d = null;

The line below however won't compile:

String a = b = c = d = null;  // illegal

(Note that if these are member variables, they will be initialized to null automatically.)