Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8517435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:42:02+00:00 2026-06-11T05:42:02+00:00

In Scala, if we have class Foo(bar:String) We can create a new object but

  • 0

In Scala, if we have

class Foo(bar:String)

We can create a new object but cannot access bar

val foo = new Foo("Hello")
foo.bar // gives error

However, if we declare Foo to be a case class this works:

case class Foo(bar:String) 
val foo = Foo("hello")
foo.bar // works

I am forced to make many of my classes as case classes because of this. Otherwise, I have to write boilerplate code for accessing bar:

class Foo(bar:String) {
  val getbar = bar
}

So my questions are:

  1. Is there any way to “fix” this without using case classes or boilerplate code?
  2. Is using case classes in this context a good idea? (or what are the disadvantages of case classes?)

I guess the second one deserves a separate question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T05:42:04+00:00Added an answer on June 11, 2026 at 5:42 am

    Just use val keyword in constructor to make it publicly accessible:

    class Foo(val bar:String) {
    
    }
    

    As for your question: if this is the only feature you need, don’t use case classes, just write with val.

    However, would be great to know why case classes are not good.

    In case classes all arguments by default are public, whereas in plain class they’re all private. But you may tune this behaviour:

    scala> class Foo(val bar:String, baz: String)
    defined class Foo
    
    scala> new Foo("public","private")
    res0: Foo = Foo@31d5e2
    
    scala> res0.bar
    res1: String = public
    
    scala> res0.baz
    <console>:10: error: value baz is not a member of Foo
                  res0.baz
    

    And even like that:

    class Foo(private[mypackage] val bar:String) {
      // will be visible only to things in `mypackage`
    }
    

    For case classes (thanks to @JamesIry):

    case class Bar(`public`: String, private val `private`: String)  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a number of classes that look like this: class Foo(val:BasicData) extends Bar(val)
Suppose I have the following Scala code: class Foo(a: Int) class Bar(b: Buffer[Int]) extends
I have a scala class: class Foo { def this(st: String) { var baz
I have a type like this sealed class Foo[A](val value: A) object Foo {
In Scala, we can write object Foo { def bar = {} } How
Suppose I have a the following in Scala object Foo { var functions: List[String
I have a class that I would like to use in a scala.collection.mutable.PriorityQueue, but
In Scala IDE I have the following code: package pkg.one object S { class
In Scala, if I have a simple class as follows: val calc = actor
In Scala, I can make a caseclass, case class Foo(x:Int) , and then put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.