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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:52:51+00:00 2026-05-30T22:52:51+00:00

In Scala, it’s possible to define class attributes in the constructor. But once you

  • 0

In Scala, it’s possible to define class attributes in the constructor. But once you declare them there, it’s not possible anymore to change the behavior (getters and setters), like you can when declaring in the class body?

Example:

class MyExample(var attribute : String)
{
    def attribute() //trying to override getter <- doesn't work
}

class MyExample(theAttribute : String)
{
   def attribute = theAttribute //overriding default accessor (was var)
}

If it’s not possible, why is it so? It seems confusing when you show people that they can easily create attributes by setting var or val in constructor, and not care about getters and setters (they can change if they need), to ultimately find that, in fact, you should avoid using those kind of facilitations.

  • 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-30T22:52:53+00:00Added an answer on May 30, 2026 at 10:52 pm

    Let’s for a moment imagine that it was possible to override the generated accessor-method:

    class MyExample(var attribute : String)
    {
        def attribute() = attribute + "abc" // won't compile!
    }
    

    Without a further qualification it is impossible to tell whether the name attribute within method body refers to the class field or recursively to the method itself.

    By design in Scala methods and fields belong to the same namespace, this, known as Uniform Access Principle, gives the ability to change an internal implementation without breaking the external interface.

    An initial implementation could be:

    class MyExample(val attribute : String)
    

    Then changed onto:

    class MyExample (attr :String) {
       def attribute = attr toUpperCase 
    }
    

    And then

    class MyExample(var attribute : String)
    

    Or

    class MyExample(attr : String) { 
        def attribute = attr toUpperCase 
        def attribute_= (a : String) { attr = a + "abc" } 
    }
    

    All without breaking any of the dependent code.

    When field is defined as part of a constructor the automatically generated accessor-methods are syntactic sugar. The sugar eases quick prototyping and helps to keep the code concise. Nonetheless, whenever you want to add more substance you must use the full syntax.

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

Sidebar

Related Questions

In Scala, a class's primary constructor has no explicit body, but is defined implicitly
Scala's handling of superclass constructor parameters is confusing me... with this code: class ArrayElement(val
In Scala, how does one define a local parameter in the primary constructor of
In scala it is possible to define a local block in a function. The
Scala provides a choice of different testing frameworks, but I wonder if there are
In Scala, if I define a method called apply in a class or a
Scala includes the continuations plugin now (yay), but must be enabled by passing -P:continuations:enable
In Scala, is it possible to get the string representation of a type at
In Scala, if I have a simple class as follows: val calc = actor
Does Scala have a version of Rubys' each_slice from the Array class?

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.