Call scala abstract classes with parameters and inner classes from Java

If I define a Scala class:

 class X(i:Int) {
   println (i)
 }

How to use this class in java code?

[EDIT] In fact, my problem is a little more complicated

I have an abstract class

 abstract class X(i:Int) {
   println (i)
   def hello(s:String):Unit
 }

I need to use this in Java code. Can this be done easily?

[EDIT2] Consider the following code

 object B {
    case class C(i:Int)
 }
 abstract class X(i:Int) {
   println (i)
   def hello(a:B.C):Unit
 }

In this case, the following java code throws an error in the Netbeans IDE, but it builds fine:

 public class Y extends X  {
    public void hello(B.C c) {
       System.out.println("here");
    }
    public Y(int i) {
       super(i);
    }
 }

The error I get is:

hello(B.C) in Y cannot override hello(B.C) in X; overridden method is static, final

Netbeans 6.8, Scala 2.8.

At the moment, I believe that the only solution is to ignore NB errors.

Here is the image showing the exact error (s) I get: IDE error using scala code from java

+3
source share
3 answers

- Java:

abstract class X implements scala.ScalaObject {
  public X(int i) {
    System.out.println(i);
  }

  public abstract void hello(String s);

  //possibly other fields/methods mixed-in from ScalaObject
}

, Java; hello.

+7

java- Scala , Java, Scala. :

dcs@ayanami:~/tmp$ cat X.scala
abstract class X(i:Int) {
   println (i)
   def hello(s:String):Unit
}

dcs@ayanami:~/tmp$ scalac X.scala
dcs@ayanami:~/tmp$ cat Y.java
public class Y extends X {
    public Y(int i) {
        super(i);
    }

    public void hello(String s) {
        System.out.println("Hello "+s);
    }
}
dcs@ayanami:~/tmp$ javac -cp .:/home/dcs/github/scala/dists/latest/lib/scala-library.jar Y.java
+3

Scala Java (, - ) ..). , ?

X x = new X(23);

javap , , w60 . , Scala, Java- (, implicits).

+1

All Articles