• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

How can Scala Case class be used as POJO (as in Java) as it is immutable?

 
Ranch Hand
Posts: 1257
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scala Case  class is immutable. If we use it as POJO of Java, then we will not be able to set the variable again because it is immutable. How can it be used a a POJO of Java in such a case where setting is not possible?

Thanks
 
Rancher
Posts: 4271
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's nothing about a POJO that says it cannot be immutable.

A String is a POJO after all.
 
Monica Shiralkar
Ranch Hand
Posts: 1257
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case is there any benefit in having scala case class as POJO instead of Scala class?
 
Rancher
Posts: 3371
31
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, case classes can be defined quickly and concisely, with ready-made implementations of equals(), hashCode(), and toString(), and they can be used in pattern matching.  Those are all nice benefits.

Also, nothing actually prevents you from putting mutable fields in a case class.  Scala likes to encourage and facilitate using immutable instances whenever possible, but if you need mutability, you can get it.
 
Monica Shiralkar
Ranch Hand
Posts: 1257
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
No matter how many women are assigned to the project, a pregnancy takes nine months. Much longer than this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!