0

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)



             }
           }

...

3
  • 2
    before functionname is 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. Commented Jun 13, 2022 at 13:04
  • 3
    Simple solution to your problem: do not use vars. Commented Jun 13, 2022 at 13:04
  • Thanks for your answer, the code I have written is a very simple summary of my problem, the application has been made by third parties and I have to continue where they have finished it. Paths are read from a Json file and writing their argument returns a value, sometimes it's a path and other times it's a unique identifier, my problem is that it's only in that class and in that function where I can get them, and now I need it for other classes, I know it doesn't meet the Scala standards but the client doesn't care. Commented Jun 13, 2022 at 14:39

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

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.