Scala - Import

Card Puncher Data Processing

About

In Scala, everything can be imported to make code available in your code (not only class names)

Management

when importing all the names of a package or class, scala uses the underscore character _ instead of the java asterisk *. That’s because the asterisk is a valid Scala identifier (e.g. method name)

Package

package foo.bar will be available in your code.

import foo.bar._

Object

With the object baz in package foo.bar, the following would import all the members of that object.

import foo.bar.baz._

The import statement imports all members of the foo.bar.baz class. This makes visible:

  • the static method
  • and the static fields

Class

If you define a class MyClass in package foo.bar, you can import that specific class (and not anything else from that package) with

import foo.bar.MyClass.

Multiple classes can be imported from the same package by enclosing them in curly braces:

import java.util.{Date, Locale}





Discover More
Card Puncher Data Processing
Scala - Java

All classes from the java/lang/package-summaryjava.lang package are imported by default, while others need to be imported explicitly. Java’s class libraries define powerful utility classes, such...



Share this page:
Follow us:
Task Runner