Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Scala classes be used in Java

Tags:

scala

class Wish{
    val s = "Hello! User.  Wish you a Great day."
}

object Wish{
    def main(args: Array[String]){
        val w = new Wish()
        println("Value - " + w.s )
   }
}

Java classes can be used in Scala. Similarly, can Scala classes be used in Java?

like image 200
Kannan Avatar asked Jul 09 '17 04:07

Kannan


1 Answers

Yes. If you want to do this, there are a few things you might want to remember:

  • Do not use operators in your method names or provide a wordy alternative. Operator names can be called from Java but are mangled into somethings very ugly.
  • Java users might expect Java style getters and setters. You can produce those automatically by adding @BeanProperty annotation to fields.
  • In the same way Java user might be accustomed to factory methods called ClassName.of where Scala uses .apply. Those you have to provide by hand, if you want to provide that service.
like image 64
Stefan Fischer Avatar answered Nov 15 '22 05:11

Stefan Fischer