cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

What's the difference between object and class in Scala?

Kaniz_Fatma
Community Manager
Community Manager
 
1 ACCEPTED SOLUTION

Accepted Solutions

dazfuller
Contributor III

A class is the definition and you can create many instances of them, just like classes in any other language. An object is the instance of the class, a singleton, and can be used to create features you might recognise as static methods.

Often when writing Scala you'll define a class and then create a backing object where you might create things like factory methods. One example might be that you have a class and want to be able to create an instance from a string value. In this case you would create an object with a "from" function that takes a string, and returns a new instance of the class.

myVal: MyClass = MyClass.from("some value")

View solution in original post

2 REPLIES 2

dazfuller
Contributor III

A class is the definition and you can create many instances of them, just like classes in any other language. An object is the instance of the class, a singleton, and can be used to create features you might recognise as static methods.

Often when writing Scala you'll define a class and then create a backing object where you might create things like factory methods. One example might be that you have a class and want to be able to create an instance from a string value. In this case you would create an object with a "from" function that takes a string, and returns a new instance of the class.

myVal: MyClass = MyClass.from("some value")

Thank you dazfuller(Customer).

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group