It is possible to use Scala classes in Java

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 in Java be used?

+3


source to share


2 answers


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



  • Don't use operators in method names or provide an alternative. Operator names can be called from Java, but they are mangled into something very ugly.
  • Java users can expect Java style getters and setters to be used. You can create them automatically by adding annotations @BeanProperty

    to the fields.
  • In the same way, a Java user can get used to factory methods ClassName.of

    where Scala uses .apply

    . The ones that you have to provide manually if you want to provide this service.
+3


source


Yes, Scala classes can be called from Java and vice versa.

Below is the text: Scala Frequently Asked Questions



What does it mean that Scala is Java compatible?

The standard Scala backend is a Java virtual machine. Scala classes are Java classes and vice versa. You can call methods of any language from methods in another. You can extend Java classes in Scala and vice versa. The main limitation is that some Scala functions have no Java equivalents, such as damn.

The following post may also be helpful to you: How to invoke Scala from Java

+2


source







All Articles