Scala

Scala Case Class

The Case class in Scala is pretty much like a regular Scala class but with some additional functionality. The objects of this class can be instantiated even without using the “new” keyword. Moreover, we can conveniently copy one object of the Case class to another entirely or even while changing some of the values of some of the attributes of this class. This article will be dedicated to using the Case class in Scala.

Syntax of the Case Class in Scala:

The general syntax of the Scala Case class is given below:

case class NameOfTheClass(Parameters of the Case Class)

Here, “case” and “class” are the keywords that are used for declaring a Scala Case class. Then, “NameOfTheClass” can be any meaningful name that you want for your class. Finally, the “Parameters of the Case Class” should be replaced by the actual attributes of that you want for your Case class.

Instances of Using the Case Class in Ubuntu 20.04:

For learning how to use the Case class in Scala in Ubuntu 20.04, you will have to understand the following examples:

Example # 1: Creating and Accessing the Object of the Case Class:

This is a very basic illustration of utilizing the Case class in the Scala programming language. In this illustration, we will simply create a Case class and instantiate an object of this class. After that, we will access this object by printing the attributes of this class on the terminal. The sample Scala script for achieving this objective is as follows:

You can see in the Scala script shown above that we began with creating a Case class with the title “student” and have assigned to it two different attributes, i.e., “name” and “age”. One of these attributes is of “String” data type, and the other one is of “Integer” data type. Then, we have created a normal Scala class “main” and have also defined our “main()” driver function. After that, we have created an object of our Case class by using the “var” keyword and assigned values to the attributes of this class by using the name of this class. Finally, we have printed the attributes of this class on the terminal with two “println” statements.

For making an executable file of this script, we used the command given below:

$ scala CaseClass.Scala

Then, we executed this Scala program with the following command:

$ scala Main

Upon execution, the values of the attributes of our Case class were printed on the terminal as shown in the image below:

Example # 2: Copying One Object of the Case Class to another Object of the Case Class:

In this illustration, we will simply create a Case class and instantiate an object of this class. After that, we will access this object by printing the attributes of this class on the terminal. Then, we will copy this object to another object of this class, and to find out if the copying process has taken place successfully or not, we will print the attributes of this object too on the terminal. The sample Scala script for achieving this objective is as follows:

This Scala script is exactly like our first script till the instantiation of the first object and printing the associated attributes on the terminal. After that, we have simply created another object and copied the first object into it by using the “var s2=s1.copy()” notation. After that, we tried to print the values of the second object on the terminal to verify if the first object had been copied to the second one successfully or not.

You can verify from the output of this modified Scala script that our first object of the Case class has successfully been copied to the second one, as shown in the image below:

Example # 3: Copying One Object of the Case Class to another Object of the Case Class while Changing Some Parameters:

This example is somewhat similar to our second example. In this illustration, we will simply create a Case class and instantiate an object of this class. After that, we will access this object by printing the attributes of this class on the terminal. Then, we will copy this object to another object of this class, and to find out if the copying process has taken place successfully or not, we will print the attributes of this object too on the terminal. However, this time while copying one object to another, we will change one of its attributes. The sample Scala script for achieving this objective is as follows:

This Scala script is pretty much like our second script; however, while copying our first object to the second object of the Case class, we have passed a different value for one of the attributes, i.e., age in our case. After that, we tried to print the values of the second object on the terminal to verify if the first object had been copied to the second one successfully or not with a modified attribute.

You can verify from the output of this modified Scala script that our first object of the Case class has successfully been copied to the second one with one modified attribute, as shown in the image below:

Conclusion:

We wanted to explain to you the usage of the Case class in Scala in Ubuntu 20.04 with the help of this article. To do this, we first shared with you some of the features of the Case class in Scala, followed by its general syntax. Then, we thoroughly explained three different examples that can help you use the Scala Case class effectively while using a Ubuntu 20.04 system.

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.