Start learning free

LESSON 204 OF 414 · FUNCTIONS & CLASSES

Defining a Class in Scala

Lesson Read the full explanation

TL ; DR:

  • A class is a blueprint for creating objects. It groups together related data and behavior under a single name.


  • In Scala, you define a class with the class keyword, followed by the class name and a pair of curly braces:

    class Dog {
    
    }
    

    The body of the class can be empty — that's still a valid class. We'll add data and behavior to it in the next lessons.



Full lesson:

So far, you've worked with built-in types like Int, String, and arrays. But what if you want to model something the language doesn't have a type for — like a Dog, a BankAccount, or a Game?

That's what classes are for. A class lets you define your own type by listing what data it holds and what actions it can perform.


Why classes?

Imagine you're writing a program about dogs. Each dog has a name, an age, and can bark. Without classes, you'd have to track each dog's name in one variable, age in another, and write a separate bark() function for each. That gets messy fast.

With a class, you bundle all of that into one neat package called Dog:

class Dog {
    // We'll add fields (data) and methods (behavior) here in upcoming lessons.
}

The syntax

Defining an empty class in Scala looks like this:

class ClassName {
}

Three things to notice:

  • The keyword class tells Scala you're defining a new type.

  • ClassName is the name you choose. By convention, Scala class names use PascalCase — capitalized words with no underscores: Dog, BankAccount, HighScore.

  • The curly braces { } hold the body of the class. They can be empty for now.


Your task

Define an empty class called Dog. The pre-filled code in mainFunction will create an instance of your class and print a confirmation message. If you define the class correctly, the program will compile and print:

Defined the Dog class!
STEP 1 OF 1
    Ctrl + Enter to check

    SCALA · FUNCTIONS & CLASSES

    Defining A Class in Scala, explained

    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 class tells 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, BankAccount or HighScore.
    • 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 Dog describes dogs, and Dog() makes one. Every call creates a new object: Dog() == Dog() is false for 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. Dog and dog are different names. Class names start with a capital letter and variables start with a lowercase one, which is why val dog = Dog() reads naturally.
    • Keywords from other languages. There is no public keyword in front of class. 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.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free