asda asda - 1 year ago 61
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?)

Answer Source

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.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download