
- Scala - Home
- Scala - Overview
- Scala - Features
- Scala - Environment Setup
- Scala - Build Tool (SBT)
- Scala - REPL
- Scala - Dot & Dotty
- Scala - Basic Syntax
- Scala - Hello World Program
- Scala - Identifiers
- Scala - Keywords
- Scala - Comments
- Scala - Code Blocks
- Scala - Semicolon
- Scala - Constructs
- Scala - Expressions
- Scala - Input and Output
- Scala - Optional Braces
- Scala - Underscore (_)
- Data Types and Variables
- Scala - Data Types
- Scala - Type Bounds
- Scala - Context Bound
- Scala - Variances
- Scala - Type Hierarchy
- Scala - Variables
- Scala - Variable Scopes
- Scala - Literals
- Scala - Numeric Types
- Scala - Boolean Types
- Scala - Char Type
- Scala - Unit Types
- Scala - Strings
- Scala - Arrays
- Scala - Null Type
- Scala - Nothing
- Scala - Any Type
- Scala - AnyRef Type
- Scala - Unified Types
- Scala - Dates and Times
- Scala - Ranges
- Scala - Multidimensional Arrays
- Scala - WrappedArray
- Scala - StringBuilder
- Scala - String Interpolation
- Scala - StringContext
- Scala - Type Casting
- Scala var vs val
- Scala Operators
- Scala - Operators
- Scala - Rules for Operators
- Scala - Arithmetic Operators
- Scala - Relational Operators
- Scala - Logical Operators
- Scala - Bitwise Operators
- Scala - Assignment Operators
- Scala - Operators Precedence
- Scala - Symbolic Operators
- Scala - Range Operator
- Scala - String Concatenation Operator
- Scala Conditional Statements
- Scala - IF ELSE
- Scala - IF-ELSE-IF-ELSE Statement
- Scala - Nested IF-ELSE Statement
- Scala Loop Statements
- Scala - Loop Statements
- Scala - while Loop
- Scala - do-while Loop
- Scala - Nested Loops
- Scala - for Loop
- Scala - break Statement
- Scala - yield Keyword
- Scala Classes & Objects
- Scala - Classes & Objects
- Scala - Constructors
- Scala - Auxiliary Constructor
- Scala - Primary Constructor
- Scala - This Keyword
- Scala - Nested Classes
- Scala - Getters and Setters
- Scala - Object Private Fields
- Scala - Singleton Object
- Scala - Companion Objects
- Scala - Creating Executable Programs
- Scala - Stateful Object
- Scala - Enumerations
- Scala - Polymorphism
- Scala - Access Modifiers
- Scala - Apply Method
- Scala - Update Methods
- Scala - UnapplySeq Method
- Scala - Inheritance
- Scala - Extending a Class
- Scala - Method Overloading
- Scala - Method Overriding
- Scala - Generic Classes
- Scala - Generic Functions
- Scala - Superclass Construction
- Scala Methods & Functions
- Scala - Methods
- Scala - Functions
- Scala - Methods vs Functions
- Scala - Main Methods
- Scala - Functions Call-by-Name
- Scala - Functions with Named Arguments
- Scala - Function with Variable Arguments
- Scala - Recursion Functions
- Scala - Default Parameter Values
- Scala - Functions without Parameters
- Scala - Implicit Parameters
- Scala - Higher-Order Functions
- Scala - Nested Functions
- Scala - Extension Methods
- Scala - Anonymous Functions
- Partially Applied Functions
- Scala - Lazy Val
- Scala - Pure Function
- Scala - Currying Functions
- Scala - Control Abstractions
- Scala - Corecursion
- Scala - Unfold
- Scala - Tail Recursion
- Scala - Infinite Sequences
- Scala - Dynamic Invocation
- Scala - Lambda Expressions
- Scala - Polymorphic Functions
- Scala Collections
- Scala - Collections
- Mutable and Immutable Collections
- Scala - Lists
- Scala - Sets
- Scala - Maps
- Scala - TreeMap
- Scala - SortedMap
- Scala - Tuples
- Scala - Iterators
- Scala - Options
- Scala - NumericRange
- Scala - Infinite Streams
- Scala - Parallel Collections
- Scala Advanced Types
- Scala - Union Types
- Scala - Intersection Types
- Scala - Type Aliases
- Scala - Structural Types
- Scala - Match Expression
- Scala - Singleton Type Operator
- Scala - Abstract Types
- Scala - Dependent Types
- Scala - Abstract Type Bounds
- Scala - Higher-Kinded Types
- Scala - Opaque Type Alias
- Scala - Path-Dependent Types
- Scala - Type Lambdas
- Scala - Type Inference
- Scala - Algebraic Data Types
- Scala Pattern Matching
- Scala - Pattern Matching
- Scala - Guards
- Scala - Variables in Patterns
- Scala - Type Patterns
- Scala - The Matchable Trait
- Scala - Matching Arrays
- Scala - Matching Lists
- Scala - Matching Tuples
- Scala - Exception Handling
- Scala - Extractors
- Scala - Pattern Bindings
- Scala - Regular Expressions
- Scala - Case Classes
- Scala - Partial Functions
- Scala - Packaging and Imports
- Scala - Implicit Imports
- Scala - Export Clauses
- Scala - Nested Packages
- Scala - Chained Packages
- Scala - Package Objects
- Scala Files I/O
- Scala - Files I/O
- Scala - Writing Files
- Scala - Listing Files
- Scala - Deleting Directories
- Scala - Check File Exists
- Scala Advanced Concepts
- Scala - Closures
- Scala - Futures
- Scala - Promises
- Scala - Traits
- Scala - Trait Mixins
- Scala - Layered Traits
- Scala - Trait Linearization
- Scala - Sealed Traits
- Scala - Transparent Traits
- Scala - Process Management
- Scala - Scaladoc
- Scala - Literal Type Arithmetic
- Scala - Inline keyword
- Scala - Def, Var & Val
- Scala - Dropped Features
- Scala Unit Testing
- Scala - Unit Testing
- Scala - uTest
- Scala - MUnit
- Scala - ScalaTest Runner
- Scala - ScalaMock
- Scala - JUnit
- Scala - Mocking
- Scala - BDD Testing
Scala - Packages and Imports
Scala uses packages to create namespaces. So, you can modularize programs and prevent naming conflicts. Import statements bring required classes and types into scope. We will discuss packaging and imports in this chapter.
Creating a Package
Packages in Scala are declared at the top of a Scala file using the package keyword. Packages group related classes, traits, and objects together. So, you can organize code and control scope.
Basic Package Declaration
This is simple package declaration in Scala -
package users class User
By convention, package names should be all lowercase, and the directory structure mirrors the package hierarchy. For example, the directory structure for the above package looks like this -
- ExampleProject - build.sbt - project - src - main - scala - users User.scala UserProfile.scala UserPreferences.scala - test
Nested Packages
You can also nest packages. It provides greater control over scope and encapsulation. You can nest packages in two ways -
1. Using curly braces
package users { package administrators { class AdminUser } package normalusers { class NormalUser } }
2. Using colons and indentation (Scala 3 syntax) -
package users: package administrators: class AdminUser package normalusers: class NormalUser
Package Naming Conventions
There is some naming conventions for code developed within an organization. You should use the organization domain name in reverse as part of the package name. For example,
package com.tutorialspoint.selfdrivingcar.camera class Lens
This will correspond to the directory structure -
SelfDrivingCar/src/main/scala/com/tutorialspoint/selfdrivingcar/camera/Lens.scala
Importing in Scala
Import statements bring required classes, traits, objects, and methods into scope. Scala import technique is more flexible than Java import technique. You can import anywhere in the code and provide features like renaming and hiding members.
Basic Imports
You can import a single class, multiple classes, and even all classes from a package. For example -
import users.User import users.{User, UserPreferences} import users._
Aliasing Imports
You can rename imported members to avoid naming conflicts and to improve clarity. For example -
import java.util.{Date => UtilDate} import java.sql.{Date => SqlDate}
Hiding Members on Import
You can hide specific members while importing the rest of the package. For example -
import java.util.{Date => _, _}
Importing Static Members
You can import static members of a class directly. For example −
import java.lang.Math._ val a = sin(0) val b = cos(PI)
Importing Givens (Scala 3)
In Scala 3, you can import given instances (similar to implicit values in Scala 2). For example -
object A: given tc: TC def f(using TC) = ??? object B: import A.{given, *}
Packages in Scala
Multiple Packages in the Same File
There can be multiple packages in a single file using curly braces in Scala. For example -
package users { package administrators { class AdminUser } package normalusers { class NormalUser } }
Package Stacking
You can stack package declarations to create nested packages without curly braces. For example -
package com.tutorialspoint.scala package packageimport package stacking
You can access classes from any of the stacked packages without more import statements.
Using Packages in Different Files
You can add members to the same package from different files in Scala. It is used for organizing large codebases. For example -
// file: faculty.scala package college class Faculty { def facultyMethod() {} } // file: student.scala package college class Student { def studentMethod() {} } // file: main.scala package college object Main { def main(args: Array[String]): Unit = { val student = new Student() val faculty = new Faculty() // Methods from both Student and Faculty can be used here } }
Import Statements Anywhere
Scala's import statements can be placed anywhere in the code, not just at the top of the file. So you can have more granular control over imports. It reduces scope pollution. For example -
package foo class ClassA { import scala.util.Random def printRandom(): Unit = { val r = new Random println(r.nextInt()) } } class ClassB { // Random is not visible here // val r = new Random // This will not compile }
Package Objects
You can define functions, variables, and types that are accessible to all members of the package. Each package can have one package object, typically named package.scala. For example -
Example
package com.tutorialspoint.scala package object packageimport { val year = "2020" trait Motor { val dieselMessage: String = "I am not environment friendly" val noDieselMessage: String = "I am environment friendly" } }
Members of the com.tutorialspoint.scala.packageimport package can now access year and Motor without importing these.
object bmwB38 extends Motor { def run(): Unit = println(s"I am a bmwB38! $noDieselMessage") }
Combining Different Import Techniques
Scala has flexible import system. So, you can combine various import techniques for more readable and maintainable code. For example -
package demo import java.util.{Date => UtilDate} import java.sql.{Date => SqlDate} object Demo { def main(args: Array[String]): Unit = { val utilDate = new UtilDate() val sqlDate = new SqlDate(System.currentTimeMillis()) println(s"Util Date: $utilDate") println(s"SQL Date: $sqlDate") } }
Packages and Imports Summary
- Scala uses packages to create namespaces. So, you can modularize code and prevent naming conflicts.
- Import statements can be used anywhere in the code and for flexible importing, renaming, and hiding of members.
- Package objects let you define methods, variables, and types that are accessible to all members of the package.
- Givens in Scala 3 can be imported using the given
- You can use package stacking to create nested packages without more import statements.
- You should use naming conventions to organize packages and keep clean directory structure.
- Scala import system is more flexible than Java import system for better control over namespace and scope.