wood burning stoves 2.0*
The moose likes Scala and the fly likes Case Classes? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Languages » Scala
Bookmark "Case Classes?" Watch "Case Classes?" New topic
Author

Case Classes?

Teena George
Ranch Hand

Joined: May 01, 2008
Posts: 82
Hi,

I am learning Scala. I am kind of stuck with the concept of Case Classes. Could somebody please explain me this in simple terms?
Do we have something similar in Java? I ask because I am a Java programmer and it would be easier to understand this if we have something similar in Java.

Thanks in advance,
Teena
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
There is no real Java analog to case classes. Case classes in Scala are regular classes with some sytactic sugar added. Specifically, the compiler a toString method, and it generates a companion object with 'special' apply and unapply methods. The key thing to remember with case classes is that you don't have to use the keyword 'new' to create an instance (because of the compiler generated apply method), and they can be used out of the box in pattern matches (because of the compiler generated unapply method).


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Teena George
Ranch Hand

Joined: May 01, 2008
Posts: 82
Thanks Garret. I got your point.

Regards,
Teena
Teena George
Ranch Hand

Joined: May 01, 2008
Posts: 82
Hi,

Is Case Class very commonly used?
Where can I study scala pattern matching in detail? I am going through the details of scala-lang.org site and the pdf files (Scala tutorial) from the site. Is there any simpler and elaborative explanation available? I am kind of weak in pattern matching.

Thanks,
Teena
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Case classes are quite common in Scala, as is pattern matching. The most common cases are pattern matching on Options or Lists. Using a case class for pattern matching in your own class heirarchy is realtively straightforward.

Teena George
Ranch Hand

Joined: May 01, 2008
Posts: 82
Thanks Garrett. I will try it out.

Regards,
Teena
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

I would say there is a relation to Enumerations in Java, but just a relation.


http://home.arcor.de/hirnstrom/bewerbung
Teena George
Ranch Hand

Joined: May 01, 2008
Posts: 82
Thanks Stefan.
Inca Nauthiz
Greenhorn

Joined: Nov 21, 2008
Posts: 8
I wouldn't recommend overusing case classes. Scala Tips mentioned inheritance ambiguities with case classes.
In my little experience case classes help in small matters, in such points, where no extensions are planned (for example, in domain model parts, to avoid equals/hashCode boilderplates). They greatly increase readability in such cases. On the other hand, using them just to save three characters from unnecessary "new" keyword may introduce other concerns. Use them wisely.


If you use Scala, please try my Web framework and ORM and let me know, what you think about it. TIA.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Case Classes?
 
Similar Threads
inserting xml string into oracle database
Creating Spring XML beans file dynamically
Regarding Facade Pattern implementation
Regarding maximum length a user can enter.
More than one portlets in a single war file