3

If I define a Scala class:

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

How do I use this class in Java code?

[EDIT] Actually, my problem is slightly 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. Is it possible to do it 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 gives an error in Netbeans IDE but 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.

As of now I think the only solution is to ignore the NB errors.

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

3
  • FYI, this question is very badly written. To start, you did not show an example of what you really were having trouble with -- and given the comment about inner class, you didn't show it even in the edit. Next, you didn't say what you have actually tried to do, and, finally, you didn't explain what errors you were seeing or what you didn't know how to do. Remember: better questions, better and faster answers. Commented Jun 4, 2011 at 14:18
  • 1
    Hi Daniel, Sorry for that. Actually the error was weird and had me stumped. It seems to be a bug in Netbeans IDE or the Scala plugin, as the code compiles fine, just gives errors in the editor. I am using NB 6.8, Scala 2.8. I have updated my question with the example and error. Commented Jun 4, 2011 at 15:24
  • Well, now the question is very good! :-) Commented Jun 6, 2011 at 16:15

3 Answers 3

7

The generated bytecode for your class will be identical to the Java definition:

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
}

Use it exactly as you would for this equivalent Java; subclass and provide a concrete implementation of the hello method.

Sign up to request clarification or add additional context in comments.

3 Comments

Actually, it implements scala.ScalaObject as well, which means javac needs Scala's library in the class path.
Yes, it works. The only issue is that Java cannot call inner classes in Scala. So some refactoring was necessary.
Update the above comment. Refactoring was not necessary as it seems to be a bug in NB. However, after refactoring (moving inner classes outside), the IDE errors went away.
3

You need to pass the Scala library jar in the class path to compile any Java code extending a Scala class. For example:

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

2 Comments

It's fair to assume that the OP was already doing this, given that the first example worked :)
@Kevin Well, he didn't say what wasn't working, now did he?
1

You should be able to call Scala code from Java (maybe with some name mangling and so on). What happens if you try the following?

X x = new X(23);

In more complicated cases you can use javap or similar tools to find out how the Scala code got translated. If nothing else works, you can still write some helper methods on Scala side to make access from Java side easier (e.g. when implicits are involved).

1 Comment

This works in the first example. However, I am having problem for the second case

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.