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

  • Home
  • SEARCH
  • 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 9073721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:28:58+00:00 2026-06-16T18:28:58+00:00

Coming from a java background I always mark instance variables as private. I’m learning

  • 0

Coming from a java background I always mark instance variables as private. I’m learning scala and almost all of the code I have viewed the val/var instances have default (public) access. Why is this the access ? Does it not break information hiding/encapsulation principle ?

  • 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-16T18:28:59+00:00Added an answer on June 16, 2026 at 6:28 pm

    It would help it you specified which code, but keep in mind that some example code is in a simplified form to highlight whatever it is that the example is supposed to show you. Since the default access is public, that means that you often get the modifiers left off for simplicity.

    That said, since a val is immutable, there’s not much harm in leaving it public as long as you recognize that this is now part of the API for your class. That can be perfectly okay:

    class DataThingy(data: Array[Double) {
      val sum = data.sum
    }
    

    Or it can be an implementation detail that you shouldn’t expose:

    class Statistics(data: Array[Double]) {
      val sum = data.sum
      val sumOfSquares = data.map(x => x*x).sum
      val expectationSquared = (sum * sum)/(data.length*data.length)
      val expectationOfSquare = sumOfSquares/data.length 
      val varianceOfSample = expectationOfSquare - expectationSquared
      val standardDeviation = math.sqrt(data.length*varianceOfSample/(data.length-1))
    }
    

    Here, we’ve littered our class with all of the intermediate steps for calculating standard deviation. And this is especially foolish given that this is not the most numerically stable way to calculate standard deviation with floating point numbers.

    Rather than merely making all of these private, it is better style, if possible, to use local blocks or private[this] defs to perform the intermediate computations:

    val sum = data.sum
    val standardDeviation = {
      val sumOfSquares = ...
      ...
      math.sqrt(...)
    }
    

    or

    val sum = data.sum
    private[this] def findSdFromSquares(s: Double, ssq: Double) = { ... }
    val standardDeviation = findMySD(sum, data.map(x => x*x).sum)
    

    If you need to store a calculation for later use, then private val or private[this] val is the way to go, but if it’s just an intermediate step on the computation, the options above are better.

    Likewise, there’s no harm in exposing a var if it is a part of the interface–a vector coordinate on a mutable vector for instance. But you should make them private (better yet: private[this], if you can!) when it’s an implementation detail.

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

Sidebar

Related Questions

I've recently been learning vb.net, coming from a background in java and c. I'm
I'm learning C++ coming from a Java background (knowing a little C from many
I'm just learning c++ coming from a Java background. Just playing around with simple
Coming from a non-Java background to Scala has brought me a wide range of
I'm attempting to learn Scala coming from a Java background. Should the below program
Coming from a Java background I'm learning C, but I find those vague compiler
Just learning Objective-C coming from a Java background. I am trying to write a
Coming from a Java background, I'm wondering why List in Scala doesn't have a
Coming from a Java background, I'm aware that an interface's method cannot contain code
When I was learning Java coming from a background of some 20 years of

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.