sachinmb27
sachinmb27

Reputation: 29

Can I use a variable from java file in a scala file?

As the title says, is it possible to use a java variable in a scala file?

Say I have two files, java1.java and scala1.scala in the same folder.

java1.java file,

public class java1{
    public static void main(String[] args) {
        String a = "Hello";
    }
}

scala1.scala file,

object scala1 extends App {
    val b: String = a // a is the variable from java1.java
}

Upvotes: 0

Views: 66

Answers (1)

Jörg W Mittag
Jörg W Mittag

Reputation: 369458

a is a local variable. It is local to the scope it is defined in, in that case the method java1.main. (That's why it's called a "local" variable, after all!)

You cannot even access this variable in a different method of the same file, let alone a completely different object.

This has nothing to do with Scala vs. Java. This is true for pretty much every programming language ever created.

Upvotes: 4

Related Questions