Updated July 7, 2023
Introduction to Scala Package Object
Introduced in version 2.8, Scala Package Objects contains the definition of variables, classes, and objects that are to be accessible to the entire package. These are the common objects whose scope is accessible to all the classes and objects in the entire package. So the scope of the objects is easily integrated within the application. By the use of it, we can have the common data always in one place and accessible within the scope of the application. Package objects can inherit Scala Classes and traits.
Syntax of Scala Package Object
The syntax is as follows:
packagecom.demo // package name
packageobjectobject_demo //package object name
{
vala = 5;
//other members
}
We need to create a file with the name of the 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.
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 that we want to use. Basically, the standard Scala package has its package object, as scala._ is imported. The members, objects, and variables that are needed all over the program are put over this package object which we can use without importing the package. Using the 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 objects, we can shorten the lines of code for Scala and avoid using import statements. Whenever we compile the code, it creates a 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 at an example of a package Object having the 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 the 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 integers.
Output:
So in this way, we can use the Object in our Scala Application. It also contains immutable collections that are used with the Scala Code. Iterable, Traversable, Seq, Iterator, List, Nil are the collection object in the Scala package object. Big Decimal, BigInt, Numeric are now moved in Scala.math.* package object.
Example:
We can also call a method 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 in 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 named 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 method can be used within 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 have the scope. With the help of various examples, we tried to clarify the functioning.
Recommended Articles
We hope that this EDUCBA information on “Scala Package Object” was beneficial to you. You can view EDUCBA’s recommended articles for more information.