What a class is
Scala comes with built-in types such as Int, String and Array. A class lets you add a type of your own, such as Dog, BankAccount or Game. It is a blueprint: it says what data an object of that type holds and what it can do. The objects made from it come later; this lesson is only about the blueprint.
Without classes, a program about dogs would need one variable for each dog's name, another for each age, and a separate function for each action. A class groups all of that under one name.
The syntax
The simplest class has only a name and an empty body:
class Dog {
}
Three things to notice:
- The keyword
classtells Scala that a new type is being defined. - The name comes next. By convention it is written in PascalCase: each word starts with a capital letter and there are no underscores, as in
Dog,BankAccountorHighScore. - The braces hold the body of the class. An empty body is valid, and for a class that has nothing in it the braces can be left out completely:
class Dog
Both forms define exactly the same class. Later lessons add fields (data) and methods (behavior) inside the braces.
Using the class as a type
Once Dog exists, it can be used wherever a type is expected, for example in a variable declaration. In Scala 3, calling the class name like a function creates an instance:
val dog = Dog()
val same: Dog = dog
The first line creates a Dog and stores it in dog, and Scala works out that the type is Dog. The second line states the type explicitly. Writing new Dog() is also valid and does the same thing.
Calling the name is what creates the object. The name alone is not a value: writing val d = Dog without the parentheses doesn't compile, and the compiler says that the constructor proxy object Dog cannot be used as a value.
The exercise
The lesson gives you a class named Solution whose method mainFunction already contains this code:
val dog = Dog()
println("Defined the Dog class!")
It creates a Dog and prints a message, but no class named Dog exists yet. Define it above Solution in the same file, with an empty body:
class Dog {
}
class Solution {
def mainFunction(): Unit = {
val dog = Dog()
println("Defined the Dog class!")
}
}
Now the code compiles and prints:
Defined the Dog class!
Without the Dog definition, the compiler stops with Not found: Dog, because the name inside mainFunction refers to nothing. The class needs to be somewhere in the file, but its position doesn't matter to the compiler: it can also be written below Solution.
Good to know
- A class is not an object.
class Dogdescribes dogs, andDog()makes one. Every call creates a new object:Dog() == Dog()isfalsefor an empty class, because two separate objects are never equal unless the class says so. - The file name is free. Scala doesn't require a class to live in a file with the same name, and one file can hold many classes.
- Capitalization matters.
Doganddogare different names. Class names start with a capital letter and variables start with a lowercase one, which is whyval dog = Dog()reads naturally. - Keywords from other languages. There is no
publickeyword in front ofclass. A class is public unless you restrict it.
Key takeaways
- A class is a blueprint for a new type.
- Define one with
class Name, optionally followed by braces. - Class names use PascalCase.
- An empty class is valid, and in Scala 3
Dog()creates an instance of it.
