0

class:org.apache.commons.cli.OptionBuilder

jar:commons-cli-1.2.jar

jdk: 1.8

In Java:

OptionBuilder.withArgName("abc").withDescription("bcd");

Compiled successfully

The same code in Scala:

OptionBuilder.withArgName("abc").withDescription("bcd")

Error:(7, 38) value withDescription is not a member of org.apache.commons.cli.OptionBuilder OptionBuilder.withArgName("abc").withDescription("bcd")

5
  • Can you share the complete code snippet Commented Dec 1, 2017 at 2:29
  • hello, the complete code snippet is downstairs Commented Dec 1, 2017 at 2:53
  • So did that work for you? Commented Dec 1, 2017 at 2:54
  • not work, the scala code cannot compile Commented Dec 1, 2017 at 2:54
  • stackoverflow.com/questions/4903296/… Commented Dec 1, 2017 at 3:06

1 Answer 1

2

Unfortunately, You will have to use it as :

def main(args: Array[String]): Unit = {
      OptionBuilder.withArgName("abc")
      OptionBuilder.withDescription("bcd")
    }

This is because there is no instance method withDescription in OptionBuilder, only a static method. Since withDescription is a static method, you obviously need to call it on the class, not on an instance of the class.

private static OptionBuilder instance = new OptionBuilder();
public static OptionBuilder withArgName(String name)
    {
        OptionBuilder.argName = name;

        return instance;
    }



 public static OptionBuilder withDescription(String newDescription)
    {
        OptionBuilder.description = newDescription;

        return instance;
    }
Sign up to request clarification or add additional context in comments.

3 Comments

yes, I was curious why scala can not be called that way
As far as I know in Java, static methods can be called on an instance, and if there is no corresponding instance method, instead of throwing an error, java will converts it into a static method call for us.
My god, what a horrible API.

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.