Win a copy of Head First Android this week in the Android forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

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

 
Ranch Hand
Posts: 2545
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2545
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In that case is there any benefit in having scala case class as POJO instead of Scala class?
 
Master Rancher
Posts: 4055
56
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2545
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic