5

I see this question: Scala: public static final in a class and this one, too: http://www.scala-lang.org/old/node/9178 but I can't make it work...

What I want to do is the Scala equivalent of the following Java code:

public class MyClass extends AnotherClass{

  public static final String WSDL = MyConfig.getProp("...");
  public static final String SERVICES = {new QName(MyConfig.getProp("..."))};
  public MyClass(){
    super(WSDL,SERVICES);
  }
}

What I tried, based on what I searched, is:

class MyClass (wsdl: String, services: Array[QName])(implicit val config:MyConfigClass) extends AnotherClass(wsdl,services:_*){
  val WSDL:String = config.getProp("...")
  val SERVICES: Array[QName] = Array(new QName(config.getProp("...")))
  def this() {
    this(WSDL,SERVICES:_*)
  }
}

But Scala IDE is telling me WSDL and SERVICES are unknown. Declaring WSDL and SERVICES at this point isn't supposed to be the equivalent for the Java MyClass.WSDL access?

Thanks for any solution, and for any explanation to make me scala more digestible...

1 Answer 1

8

Scala has no static. What you do instead is put everything that you would make static in Java, in the companion object of the class.

This is what your code should look like:

class MyClass (wsdl: String, services: Array[QName])
    extends AnotherClass(wsdl, services: _*) {
  def this() {
    this(MyClass.WSDL, MyClass.SERVICES)
  }
}

object MyClass {
  final val WSDL: String = "..."
  final val SERVICES: Array[QName] = Array(new QName("..."))
}
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks, but in this case, I have another problem... WSDL is not just an hard-coded String but something like config.getProp("a property"), where config is an implicit value initialized in my App object. As an object can't be initialized with any params for what I see, how could I pass my config value to the companion object? I will edit my question...
@Cheloute How would you do that with static final variables in Java? Then you also can't depend on things that passed to your constructor.
@Jasper-M you're right, my problem is with my configuration implementation now.. Well, this solution is OK, so! Thanks!
This is not 100% equivalent: constants must be explicitly marked as final val, even if they are implicitly final by being declared in an object or final class. The Scala Language Specification only guarantees constant optimizations for fields explicitly marked as final val.
@Cheloute Java's final has multiple meanings, val is only equivalent to one of them, preventing reassigning variables. final can also make methods non-overrideable and classes non-extendable. So a final val is a val that you can't override in a subclass.
|

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.