cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

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

Kaniz
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")

Kaniz
Community Manager
Community Manager

Thank you dazfuller(Customer).

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.