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 7645455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:56:48+00:00 2026-05-31T09:56:48+00:00

In Scala I see such feature as object-private variable. From my not very rich

  • 0

In Scala I see such feature as object-private variable. From my not very rich Java background I learnt to close everything (make it private) and open (provide accessors) if necessary. Scala introduces even more strict access modifier. Should I always use it by default? Or should I use it only in some specific cases where I need to explicitly restrict changing field value even for objects of the same class? In other words how should I choose between

class Dummy {
    private var name = "default name"
}

class Dummy {
    private[this] var name = "default name"
}

The second is more strict and I like it but should I always use it or only if I have a strong reason?

EDITED: As I see here private[this] is just some subcase and instead of this I can use other modifiers: “package, class or singleton object”. So I’ll leave it for some special case.

  • 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-05-31T09:56:49+00:00Added an answer on May 31, 2026 at 9:56 am

    I don’t think it matters too much, since any changes will only touch one class either way. So the most important reason to prefer private over protected over public doesn’t apply.

    Use private[this] where performance really matters (since you’ll get direct field access instead of methods this way). Otherwise, just settle on one style so people don’t need to figure out why this property is private and that one is private[this].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having problems using the update method of scala.collection.immutable.HashMap.I don't see the reason
Does Scala have a version of Rubys' each_slice from the Array class?
In Scala IDE I have the following code: package pkg.one object S { class
How do I deserialize in Psych to return an existing object, such as a
I see in the built-in class MessageQueue.scala of scala 2.7.7, around line 164, it's:
Consider such code (this is just example not real code): class Foo(url : String)
I've read a few questions such as Scala vs Haskell discussing the merits of
I tried to shrink and obfuscate my Scala/Java program using Proguard. While I was
Newbie Scala Question: Say I want to do this [Java code] in Scala: public
scala> val l = List((1,2), (2,3)) l: List[(Int, Int)] = List((1,2), (2,3)) I can

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.