I have two files:
logic.scala and main.scala
logic.scala contains one class and main.scala have one class with method main (to run it). And I want to import a class from logic.scala and use this class to create object(s) and work with them.
How to import and compile it in proper way?
Importation in Scala is a mechanism that enables more direct reference of different entities such as packages, classes, objects, instances, fields and methods. The concept of importing the code is more flexible in scala than Java or C++. The import statements can be anywhere.
With Scala you can: Import packages, classes, objects, traits, and methods. Place import statements anywhere. Hide and rename members when you import them.
import is just a syntactic feature that lets you avoid writing out the full package every time you use a classname. You do not need to import dependencies at all.
logic.scala
codepackage logic class Logic{ def hello = "hello" }
main.scala
codepackage runtime import logic.Logic // import object Main extends Application{ println(new Logic hello) // instantiation and invocation }
scalac
scalac *.scala
scala
scala -cp . runtime.Main
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With