Class Basics
In this page:
Class Basics
A Scala class is declared with class ClassName { ... }, bundling data and behavior into a blueprint you instantiate with new. Unlike some languages, a class's primary constructor parameters can be declared directly in the class signature, like class Point(x: Int, y: Int). Every value in Scala, including numbers, is technically an object of some class.
Note: Constructor parameters declared directly in the class signature avoid needing a separate explicit constructor block for the common case.
Example: Class Basics
class Dog(name: String) {
def bark(): Unit = {
println(s"$name says Woof!")
}
}
object Main extends App {
val rex = new Dog("Rex")
rex.bark()
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: