Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala, importing class

Tags:

import

scala

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?

like image 905
matiit Avatar asked Jun 19 '10 15:06

matiit


People also ask

What does import do in Scala?

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.

Can we import object in Scala?

With Scala you can: Import packages, classes, objects, traits, and methods. Place import statements anywhere. Hide and rename members when you import them.

What does importing a class do?

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.


1 Answers

  • logic.scala code
package logic  class Logic{    def hello = "hello"  } 
  • main.scala code
package runtime  import logic.Logic  // import  object Main extends Application{    println(new Logic hello) // instantiation and invocation  } 
  • compile the files with scalac
scalac *.scala 
  • run your application with scala
scala -cp . runtime.Main 
like image 107
Vasil Remeniuk Avatar answered Oct 13 '22 20:10

Vasil Remeniuk