0

I have seen many cases where we can create a Scala project with only Scala object file and have our code inside it without the need of having a Scala class file.

I am currently working in a project where the Developer has written the code in the Scala object file (which acts as an object) instead of writing in a class file. To be more precise here is the screen shot of my IDE which shows that the code is written in an object file.

enter image description here

I want to know significance of writing a code in a Scala class and object file. What is the ideal scenario to use them? There is a similar post available here which shows only the difference between a class and an object however I want to know when should I consider to use a Scala class and when to use a Scala object?

Alex Raj Kaliamoorthy
  • 2,035
  • 3
  • 29
  • 46
  • 2
    Dear down voter, it will be great if you add your comments on why you have voted my question down so that I can improve my way of asking next time. Thanks. – Alex Raj Kaliamoorthy May 30 '18 at 10:21
  • 2
    Possible duplicate of [Difference between object and class in Scala](https://stackoverflow.com/questions/1755345/difference-between-object-and-class-in-scala) – Jakub Zalas May 30 '18 at 11:15
  • @JakubZalas, if you see my question, I did not ask the difference between the class and object. I want to understand when to use them. – Alex Raj Kaliamoorthy May 30 '18 at 11:35
  • Sure. Some answers in the other question talk about it. – Jakub Zalas May 30 '18 at 13:32
  • Always start with object and see if it works. If it is that a class is necessary, you will come to know. For instance, if internal state must not be shared. For small programs, objects almost always work. – Jus12 May 30 '18 at 18:17

2 Answers2

2

Use class when you want to create the same data structure multiple times with difference values. For example, a class could represent a row of data in a database application, or a client request in a web server.

Use object when you only need a single copy of the data structure in your program. For example, the command line for a program, or a data cache.

Classes can have short lifetimes, so use them for data that comes and goes. Objects always last until the program completes, so use them for data that needs to be available permanently.

It is reasonably straightforward to convert an object into a class, so start with an object unless you know that you are going to need multiple instances of the data.

If you just want to group similar functions or data (in a namespace), put it in an object.

If you want are writing code for a software library, use classes that can be created and deleted as required.


Note that this applies to top-level objects. An object can also be used as a class member or as a local value, but in this case it operates like a class in that it can have multiple instances and may have a limited lifetime.

Tim
  • 26,753
  • 2
  • 16
  • 29
  • Until the end of what program does the object `bar` last in `def foo(): Int ={ object bar { def baz(i: Int): Int = i*i }; bar.baz(42) }` if I invoke `foo()`? – Andrey Tyukin May 30 '18 at 13:39
  • @AndreyTyukin Post that as a new question, please, rather than extending this question – Tim May 30 '18 at 13:48
  • 1
    That's a *rhetorical* question. You've stated in your answer that *"Objects always last until the program completes"*. I've immediately given you an example where an object `bar` is created and garbage-collected immediately after a function exits. How is your statement compatible with the example? – Andrey Tyukin May 30 '18 at 13:52
0

An object is something that you can only have one in your program. It's most of the time used to contain functions or objects that don't change much. It's the equivalent of static in other languages For instance:

object StringUtils {
  val LF = "\n"
  def toUpperCase(str: String) = str.toUpperCase() 
}

// usage
StringUtils.toUpperCase("foo")

On the other hand, a class is something you can create with new. You can have several instances of a class in your program

class StringContainer(str: String) {
  def toUpperCase() = new StringContainer(str.toUppercase())
}

// usage
val myString = new StringContainer("foo")
myString.toUpperCase()

For more detailed examples you can look at the scala documentation:

vdebergue
  • 2,354
  • 16
  • 19
  • thanks for your answer. Could you also let me know when should I use a class and when to consider using an object? – Alex Raj Kaliamoorthy May 30 '18 at 10:42
  • It really depends on the project, with who you work with, the use case. Classes are often preferred as it makes your life easier for testing and are mandatory when you need to group properties or data. Objects are simpler to use in code as they are global. In the end, you will need to try different solutions to make your own mind. – vdebergue May 30 '18 at 12:28