Scala

Scala Package Object

We can make Scala function fields and the other code implementation available at the package level. A package’s primary objective is to keep files modularized and easy to handle. We can maintain files in various distinct folders or directories depending on the namespace given. Sometimes, we want these variable definitions, functions, classes, or objects to be accessible to the entire package. However, keeping variables in a package is not possible. To accomplish this, we used a package object which allows us to store common data at the package top level. So, whenever we want to access the member of the class, it will be accessed from that particular package object. In the article, we will get a concept of package objects in Scala.

What is Package Object in Scala?

The package acts as a container where everything is defined inside a class. Each package has a corresponding package name in Scala. For each package, there is only one specific package object that is available to members to be put in the Scala source code. Package object enables variable definition classes or objects that can be accessible to the entire package created. To specify the package object in our code, we have first created a file with the same name as the Scala package.

How to Create Package Object in Scala?

To know how we can create a package object for the main class for accessing package object members in Scala, we should take a look at the examples. Note that all the implementation of code has been executed in the IntelliJ idea software application.

Example 1: Program of creating the package object in Scala

We have a separate one package object for the only class which accessed the package object. This should be in the same place in the same directory and folder. We have created a package object for the class in the code below.

In the above code, we have created a package.scala file which is in the package “company”. This package.scala can be accessed by a class file which is named “Mobile.scala”. The package.scala path is given as src/company/package.scala and the class file path is given as src/company/Mobile.scala. Both the package object and class file are at the same level and the package object is just for the class “Mobile.scala”. In the package object, we have to define fields that can be accessed by the class file “Mobile” later on. The variable is initialized with the string value and represented as the company name. The second variable is represented as owner value and also has a string value.

Now in the class file “Mobile”, we have some mobile phone brand names stored in a specified variable. Then, in the main method of the object “demo”, we are calling the mobile name through the print function. As you can see, we have defined another variable “company_info” which has the fields from the package object. In the print statement, we have passed the company info which will return the “company_name” and “owner_name” values. These returned values of variables are defined in the package object.

The output displayed in the below image shows campany_name and owner_name which are initialized in a variable of package object.

Example 2: Program of accessing the package object field in a class file in Scala

We create a package object for the class file of that object. In this way, the class file can access the fields declared in the package object file. Let’s have code implementation below.

In the above code example, we have a package object file for the class “car” in a package folder “license”. The package object has messages initialized as a string in a variable. These variables can be accessible to the “car” class file.

The class “car” has a variable defined as “person_age” that is set as “20”. Then, we have another object as a “demo” which the main method is called. Firstly, we have a print statement that will print the “person_age” from the class car. Then, we have an if-else conditional statement for a person’s age. If the person_age is greater than or equal to the value “18”, then we will have “msg_1” from the package object. If the condition is false, we will have a printed value of “msg_2”. Both the messages accessed are stored in the corresponding variable “message_1” and “message_2”.

As the age value passed as “20” which is greater than “18”, set in an if-else conditional expression. So, here we got the output message as “yes, you are eligible for driving”. The given condition is true.

 Example 3: Program of creating the package object in the class file in Scala

We can also create package objects in the class file. Below, we have an example code implementation where we have created a package object for a class file in the same source class file.

In the above code, we have the package name as a calculation which is in the math package folder. Also, we have the import package object for globally accessing the member of the class in the whole code. Then, we have created an object class as “example 1”. In the class object, we have initialized a variable as “a” and the second variable as “b” which is being called in the main method of object “demo”. In the main method, we have a print statement that will print the values stored in a variable “a” and “b”. Then in a variable “sum”, we have an addition operation on variables “a” and “b”. After the addition operation, the print function will return the sum of “a” and “b”. Now, we have a subtraction operation of the variables “x” and “y”. These variables are defined in the package object which is mentioned at the end of the code and called in the main method. The print function will return the value from “x” and “y” subtraction.

As you can see, the output display on the screenshot gives the sum from the class members and the difference from the package object.

Conclusion

The discussion on the package object is aimed to give you a clear demonstration of what is the use of package objects in Scala. The package object will shorten the long code line which will enhance readability. We have three examples of creating a package object in a different way for a class file of that particular object. These examples will build proficiency on the package object. Hopefully, you have good knowledge about package objects in Scala after reading this article.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.