Good morning I have a class called Application, it has a def that cannot change and returns an int, inside it I have defined a path that is a local variable, from another class called App that extends to the Application class, how can I get that variable local? I have tried to assign with a variable outside the block but its value is null. Thank you very much for helping me.
...
class App extends Application {
println("Inside of APP")
println(mypath)
}
class Application {
var mypath: String = _
def functionname() : Int = {
var exitCode = 0
val localPath: String = "C:/path"
mypath = localPath
println(localPath)
exitCode
}
println(mypath)
}
object HelloWorld {
def main(args: Array[String]): Unit = {
val origin = new Application()
println(origin.functionname)
}
}
The Output is:
null --> //This is value of myPath outside block (KO)
C:/path -> //This is value of Local Variable (OK)
0 --> is OK
the println of the App class is not show (KO)
I have edited the code to explain it better, you can only make the instance of val origin = new Application(), since the program allows you to do one, now what I don't know how to do is that the App class can get the local value of the class Application, but I don't get value and it doesn't show println("Inside of APP")
class App extends Application {
println("Inside of APP")
val app = new App()
app.functionname()
println(app.mypath)
}
class Application {
var mypath: String = _
def functionname(): Int = {
val exitCode = 0
val localPath: String = "C:/path"
mypath = localPath
println(mypath)
exitCode
}
println(mypath)
}
object HelloWorld {
def main(args: Array[String]): Unit = {
val origin = new Application()
println(origin.functionname())
println("===========")
println(origin.mypath)
}
}
...
functionnameis called, the value of that variable will be null. So if you call that function, and then try to print the variable, it wouldn't be null. This was just to give you some idea about your question. But generally, you shouldn't code like this in Scala, and your question is actually about fundamentals of any programming language, so in my opinion for your case it would be better to keep on learning first using some book or course.