Class

A Scala class is a template for Scala objects. A class can contain information about:

Fields

Constructors

Methods

Superclasses (inheritance)

Interfaces implemented by the class

class Greeter(prefix: String, suffix: String) {
def greet(name: String): Unit =
println(prefix + name + suffix)
}
val greeter = new Greeter("Hello, ", "!")
greeter.greet("Scala developer") // Hello, Scala developer!
class aClass {
    var aField : Int = 0;
    def this(aValue : Int) = {
        this();
        this.aField = aValue;
    }    
  }
 
val myObj=new aClass(1)
myObj.aField

/*
res35: Int = 1
*/

Case Class (You can instantiate case classes without the new keyword:)

Last updated

Was this helpful?