e0x3
e0x3

Reputation: 131

add scala code to java file

I have .java file and there is code which works. can you explain, is it possible add there some scala code, like change

    int x, y;
int xx = 0;
int yy = 0;
int z = 0;
int flag = 0;

to var x=0 var y=0 var z=0 ....

thanks

Upvotes: 0

Views: 178

Answers (1)

Rex Kerr
Rex Kerr

Reputation: 167891

Files are the smallest unit of compilation for most languages, including Java and Scala. Thus, you cannot indicate to a Java compiler that it should compile only some of the file and to the Scala compiler that it should compile other parts.

You can, however, mix classes written in Java with those written in Scala, as long as the classes are in their own appropriate files.

(Actually, it's possible to execute a line of Scala at a time using the REPL interpreter, but this can't merge the way you want with Java code because, firstly, the Java compiler doesn't know what you're doing, and secondly, the Scala is actually secretly wrapping the statements in classes to make it all work.)

Upvotes: 7

Related Questions