Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java Can i create a string that is defined as null in one line?

I'd like to pass a String parameter as "null"

However, this is for the super method in a constructor - so i cannot do

String s = null;
super(s);

I cannot just do

super(null)

as this will result in an ambiguous method call. I don't want to have an instance variable that is null for this either, that seems inelegant.

Is there a way to create a null String?

NB

 new String(null) //does not compile
like image 280
bharal Avatar asked Dec 14 '12 11:12

bharal


2 Answers

Just do this

super((String)null);
like image 187
codeMan Avatar answered Nov 15 '22 06:11

codeMan


CodeMan posted the correct answer, another solution, move away from null, and consider initializing with Empty string

super("");

this would avoid a null check later.

like image 30
AlexWien Avatar answered Nov 15 '22 05:11

AlexWien