0

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?

6
  • 1
    does it work when you cast to CharSequence in your call of isEmpty inside someMethod? Commented Jun 9, 2019 at 16:04
  • Thanks, that seems to work. Commented Jun 10, 2019 at 9:20
  • You don't need to extend from 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). Commented Jun 10, 2019 at 10:15
  • (but what you are doing should work... ) Commented Jun 10, 2019 at 10:18
  • Thanks, I know that I don't have to extend from commons-lang StringUtils. However, I do have classes that use method from my StringUtils and commons-lang StringUtils extensively, and I do not always want to write the full class name for the sake of clearness. That is where my question arises from. Commented Jun 10, 2019 at 10:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.