0

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
}
1
  • 2
    this and this should help you. Commented Dec 8, 2019 at 7:05

1 Answer 1

4

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.

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

3 Comments

Can I access if it is not a local variable?
@sachinmb27 I'd suggest you open a new question to ask that, and show exactly what code you have in mind. "is not a local variable" is vague
Okay. Thanks @SethTisue

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.