Introduction to Scala Package Object
Introduced in the version 2.8, Scala Package Objects contains the definition of variables, classes and object that are to be accessible to the entire package. These are the common objects whose scope are accessible to all the classes and objects in the entire package. So the scope of the objects are easily integrated with in the application. By the use of it we can have the common data always at one place and accessible within the scope of application. Package objects can inherit Scala Classes and trait.
Syntax of Scala Package Object
The syntax are as follow:
packagecom.demo // package name
packageobjectobject_demo //package object name
{
vala = 5;
//other members
}
We need to create a file with the name of package. Scala, where we will define this Scala Package object.
Example:
We will create a file package.scala in the same package where we want to use the package object.
And write the code for the members we want in that object package.
Code:
packagecom.demo
packageobjectobject_demo
{
vala = 5;
}
Code:
packagecom.demo.object_demo
objectDemoextendsApp {
println("hello")
println(a)
}
Call that in the main method and it will read the value that is defined in the package object.

4.5 (9,202 ratings)
View Course
Output:
Package Object:
From the above code snippet we saw how we can create the package object in scala and the syntax for this.
Package Object Working
Package objects are enclosed in a file package.Scala which is located in the same package where we want to use that. Basically the standard Scala package has their package object, as scala._ is imported. The members, objects, variables that is needed all over the program is put over this package object which we can use without importing the package. Using Package object we can have a logical structure as well as faster access to member variables.
We can import the immutable collection classes in this package object. With the use of package object we can shorten the lines of code for Scala and avoid using of import statement. Every package has one package object with the corresponding package name making it available for the members to be used in the Scala Code. Whenever we compile the code it creates class file as per the package name. So whenever we are trying to access that member it will be accessed from that package object.
Example:
Let us look with an example of package Object having SUM method in it. This displays the sum of two numbers, we will make this method in package.Scala file and will call the method in the main class to check whether we call it or not.
Code Snippet having Package.scala File in the Project Folder:
Code:
packagecom.demo
packageobjectobject_demo
{
defadd(a : Int , b: Int)
{
valc = a+b;
println("The sum of two numbers is :- "+c)
}
}
And the code for main class calling the method is:
Code:
packagecom.demo.object_demo
objectDemoextendsApp {
println("hello")
add(10,3)
}
The result will print on the sum of numbers for two integer.
Output:
So by this way we can us the Object in our Scala Application. It also contain immutable collections that are used with the Scala Code. Iterable, Traversable, Seq, Iterator, List, Nil these are the collection object in Scala package object. Big Decimal, BigInt, Numeric are now moved in Scala.math.* package object.
Example:
We can also call a method of a defined within that file and use that over our scala classes methods.
file package.Scala.
This has a method that adds up two numbers the one we saw over the above example.
Code:
packagecom.demo
packageobjectobject_demo
{
vald = 10;
defadd(a : Int , b: Int):Int =
{
valc = a+b;
println("The sum of two numbers is :- "+c)
returnc
}
}
The main method has a method naming method1 that takes the method from the package object and operates it with the variables inside it to produce the desired result.
Code:
packagecom.demo.object_demo
objectDemoextendsApp {
println("hello")
defMethod1(d:Int)
{
vale = add(10,3)
println("Insdie Method operation "+(e*d))
}
Method1(4)
// add(10,3)
}
Output:
From the above example we saw how this package object methods can be used with in our code.
Conclusion
From the above article we came to know about the usage. We came across the various benefits of having it in our object oriented code. We also saw the members are accessed with a package and has the scope. With the help of various examples we tried to clarify the functioning.
Recommended Articles
This is a guide to Scala Package Object. Here we discuss the introduction and examples of package object working respectively. You may also have a look at the following articles to learn more –