This video covers a special type of companion class called a singleton. A singleton object is used to enforce a single instance of a class. Remember, a singleton is defined as an object, not a class.
- [Narrator] In Scala, we have a special type of class,…actually, it's defined as an object,…called a singleton object.…A singleton object is used to enforce…a single instance of a class.…It's similar to using the static keyword…when you're creating a static class in Java.…A singleton is defined as an object, not a class.…What we can use it for in this application,…as you can see, I have my payroll application still up,…is that maybe we want to assign a new employee ID…every time we add a new employee.…
We need a way to create the employee ID…and maybe add one to it for all the other employees…as they come into the company.…Let's add an object that will represent the employee ID.…I'm going to do object, Employee,…and a curly bracket.…This is what we call a companion object…because it goes along with the employee class,…and inside here, I am going to create…a variable called employee…ID.…
I'll start it at zero.…Then, I'm going to define a new employee ID.…This new employee ID is going to be equal…to the employee ID…
Join Peggy Fisher as she helps get you started with Scala, so you can leverage the unique capabilities it offers. First, learn the basics of type inference, variables, loops, functions, and operators. Then, find out how to read files using a console, perform pattern matching, handle exceptions, and more. Finally, learn how to use classes, fields, methods, and objects.
- Integrating with IDEs
- Scala worksheets
- Scala repl sessions
- Type inference
- Creating variables
- Working with loops
- Higher-order functions
- Scala operators
- Working with decision statements
- Handling exceptions using try or catch
- Working with tuples and arrays
- Classes, fields, and methods
Skill Level Intermediate
Java Platforms Compared: SE vs EE vs MEwith Peggy Fisher25m 38s Intermediate
1. Getting Started with Scala
2. Scala Basics
3. More Scala Basics
4. Objects with Scala
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.