ProSyth
ProSyth

Reputation: 167

Scala get local variable from another class

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)



             }
           }

...

Upvotes: 0

Views: 719

Answers (1)

Alin Gabriel Arhip
Alin Gabriel Arhip

Reputation: 2638

You do reassign the var mypath, but you probably missed that because of the println statement inside the Application class which is called at initialization time, when you instantiate class Application with new. Your first ouput line is null because that is the default value of an uninitialized String.

Any statements written inside a class definition, that are not fields or methods (such as your println statements) will be moved inside the primary constructor of that class. As such, your println statements are called before you call method functionname, that is why mypath is null at that point.

You don't see any output from class App because you never instantiate it in your code. Of course you could do that, but since the App you would instantiate has a different instance of mypath than the one in the Application class you previously instantiated, mypath will still be null in class Application. What you need is a way to compute the value of var maypath of App, and you can to that simply by calling functioname on the App object you just instantiated, because App also inherits it.

Here's the updated example:

class App extends Application {
  println("Inside of APP")
  println(mypath)
}

class Application {
  var mypath: String = _

  def functionname(): Int = {
    val 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())
    println("===========")
    println(origin.mypath)

    val app = new App()
    app.functionname()
    println(app.mypath)

  }
}

Outputs:

null
C:/path
0
===========
C:/path
null
Inside of APP
null
C:/path
C:/path

Upvotes: 3

Related Questions