Reputation: 153
I want to declare all of them null
. 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?)
Upvotes: 14
Views: 49445
Reputation: 2137
If you're setting it to null,
String a, b, c, d;
would be enough as the declaration would set it to null by default. See this thread for further explanation.
Otherwise if you're setting a, b, c, d
to some value, you would have to use the solutions mentioned above.
Upvotes: 4
Reputation: 662
You may want something like this:
String a, b, c, d = a = b = c = null;
Upvotes: 9
Reputation: 420991
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.)
Upvotes: 26
Reputation: 1920
Yes, that's how o do it.
However, if you find yourself writing that sort of construct often in might be a sign that you are declaring a variable too early, before you know what value to put in it:
http://www.javapractices.com/topic/TopicAction.do?Id=126
Edit: You might alsdo want to look at this:
http://www.javapractices.com/topic/TopicAction.do?Id=14
Upvotes: 2
Reputation: 36456
That's perfectly valid. I suppose a slightly shorter way of doing it is:
String a, b, c, d;
a = b = c = d = null;
Upvotes: 2