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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:06:33+00:00 2026-05-27T04:06:33+00:00

Are there public instance variables anymore in Scala? I’m reading Programming in Scala ,

  • 0

Are there public instance variables anymore in Scala? I’m reading Programming in Scala, which covers Scala 2.8. If I’m understanding it correctly, it claims that vars in 2.8 are by default public.

I’m trying to write code for 2.9.1.final now, and instance variables are now by default private? But there’s no public keyword that I’m aware of. (Interestingly enough, it appears it used to exist sometime in the 2.x series, but it mysteriously disappeared somewhere along the line.)

Am I missing something obvious?

Also, by extension, is there an easy way to declare a variable passed to a class constructor to be public (since it appears that those also have default private visibility now too)?

Example:

class Instance(label: String, attributes: Array[Int]){
  val f = 0
}

Eclipse claims that label, attributes, and f are all private. Scala 2.9.1.final is being used as the library.

  • 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-27T04:06:33+00:00Added an answer on May 27, 2026 at 4:06 am

    In scala, if you omit the modifer, then instance fields are public by default:

    scala> class Foo { var foo = 1 }
    defined class Foo
    
    scala> class Bar { def bar() = { val f = new Foo; f.foo = 5; }}
    defined class Bar
    

    No worries there. However, when you use a variable in a constructor, the variable is not necessarily turned into a field:

    scala> class Foo(foo: Int)
    defined class Foo
    
    scala> class Bar { def bar() = { val f = new Foo(5); println(f.foo) }}
    <console>:8: error: value foo is not a member of Foo
           class Bar { def bar() = { val f = new Foo(5); println(f.foo) }}
                                                                   ^
    

    so you can declare it as a val or var to have it available:

    scala> class Foo(val foo: Int)
    defined class Foo
    
    scala> class Bar { def bar() = { val f = new Foo(5); println(f.foo) }}
    defined class Bar
    

    Note that all fields are actually private, but scala exposes accessor methods (foo() and foo_=(t: Int)) to enable you to access the fields), which is why scala-ide says that the fields are private (assuming you mean when you hover over the variable).

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

Sidebar

Related Questions

Is there a public/government web service that I can call to find out what
Is there a public, free web service that generates tag clouds? I'm looking for
I like to know is there any specific reason that there is no public
I like how Groovy makes all of your instance variables and class variables public
Is there any way to update instance variables from a page method without making
I regularly create a class that has a few private variables. When an instance
Is there public interface in SDK 3.* to start video recording programmatically? Also how
I wrote two methods to check there performance public class Test1 { private String
UPDATE Guid.TryParse is available in .NET 4.0 END UPDATE Obviously there is no public
My current development project has two aspects to it. First, there is a public

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.