I want to build upon org.apache.commons.lang3.StringUtils and add a certain method specific for a problem of mine. So I try to inherit from the commons class and write
package org.test.project;
public class StringUtils extends org.apache.commons.lang3.StringUtils {
public static boolean myMethod(final String cs) {
// My method body
}
}
Now, I want to use the method public static boolean isEmpty(final CharSequence cs) from org.apache.commons.lang3.StringUtils in another part of my code. As I recall and from this thread a String is a CharSequence. Thus, I write
package org.test.project.package;
import org.test.project.StringUtils;
public class TestClass{
public void someMethod(String s){
if (StringUtils.isEmpty(s)){
// do stuff
}
}
}
Why do I get the error:
cannot find symbol
symbol: method isEmpty(String)
location: class StringUtils
in my IDE?
From here I have the info that "All methods that are accessible are inherited by subclasses." Therein, it is also stated
The only difference with inherited static (class) methods and inherited non-static (instance) methods is that when you write a new static method with the same signature, the old static method is just hidden, not overridden.
But I do not have a method isEmpty with the same erasure.
Edit
So, thanks to @luksch, I added a method
public static boolean isEmpty(final String cs) {
return isEmpty((CharSequence)cs);
}
in my StringUtils class and that works. However, for testing I want to call public static String center(final String str, final int size) from the commons library. So I write
package org.test.project.package;
import org.test.project.StringUtils;
public class TestClass{
public void someMethod(String s){
if (StringUtils.isEmpty(s)){
// do stuff
}
String test = StringUtils.center("test",10);
}
}
I do not have any kind of method center my StringUtils subclass. So I thought, it would just automatically call the method from org.apache.commons.lang3.StringUtils. However, I get the equivalent error message as above:
cannot find symbol
symbol: method center(String,int)
location: class StringUtils
Why?
CharSequencein your call ofisEmptyinsidesomeMethod?extendfrom StringUtils. In fact, that will probably end up confusing things. The preferred method is to call the static methods directly (you can use static imports if you need them a lot).StringUtils. However, I do have classes that use method from myStringUtilsand commons-langStringUtilsextensively, and I do not always want to write the full class name for the sake of clearness. That is where my question arises from.