Hmmm, it makes sense to me, and I think you've made my point. I know that you can initialize String s with the literal String "". I know you can check for emptiness with s.length()==0. But if you don't want string literals in your code, how do you initialize a String to a non-null empty value without creating a bunch of duplicate empty strings? (Each call to String() creates a new char[0]). How do you initialize Strings in default constructors with String fields? If it's part of the invariant that a field be either some value or blank?
I know I can subclass it, but I don't want to, for the same reason that I don't want
a global constant or singleton. I also don't want to extend a class to a.) add static members only and b.) create a special instance of the parent class that is creatable by the parent - that's not object-oriented.
Maybe an oo way of doing what you suggest is something like:
public class EmptyStringSingleton extends String {
private static EmptyStringSingleton instance = "";
public static EmptyStringSingleton getInstance() {
return instance;
}
}
But that's super ugly. EmptyStringSingleton.getInstance();
What I need is a non-literal constant instance of a class.
And I accept the fact that it's like Collections.EMPTY_LIST, in that sometimes you just need a list with nothing in it, and you don't want to create one everytime.
Yes, maybe it's extreme that I don't want string literals in my code to the extent that I don't even want "". I suppose if you don't consider 0 a magic number, then I shouldn't consider "" a true string literal.
If a String is null the toString() value is "null", which isn't what I'm looking for. I guess what you are suggesting is that I use "" and not consider it a String literal.
But thanks for the input. Does it make no sense to you because you see an empty string as a single attribute that any String can possess?