Register Login

Scala Classes and Objects

Updated May 18, 2018

How to create Classes and Objects in Scala?

Create a Class in Scala

1) You can create a Class in Scala using the command 'case class classname()'. So for example for creating a class Student, type 'case class Student()' and Run.

 

2) You can also specify variables along with creating a class as you can see in the image below.

case class Student(var rollno : Int, var name : String, var marks: Int)

3) You can also set default values for these variables as shown below.

case class Student(var rollno : Int =1 , var name : String = "Veena" , var marks: Int = 90)

Create Object in Scala

1) To create an Object for this class Student you can create an object, for example, s1 by typing

var s1 = Student();

2) You can also specify values as shown below or it will take the default values.

var s1 = Student(4, "Mohit", 75);


 


×