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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:48:47+00:00 2026-05-17T00:48:47+00:00

Consider the following class written in Java: class NonNegativeDouble { private final double value;

  • 0

Consider the following class written in Java:

class NonNegativeDouble {
    private final double value;
    public NonNegativeDouble(double value) {
        this.value = Math.abs(value);
    }
    public double getValue() { return value; }
}

It defines a final field called value that is initialized in the constructor, by taking its parameter called alike and applying a function to it.

I want to write something similar to it in Scala. At first, I tried:

class NonNegativeDouble(value: Double) {
  def value = Math.abs(value)
}

But the compiler complains: error: overloaded method value needs result type

Obviously the compiler thinks that the expression value inside the expression Math.abs(value) refers to the method being defined. Therefore, the method being defined is recursive, so I need to state its return type. So, the code I wrote does not do what I expected it to do: I wanted value inside Math.abs(value) to refer to the constructor parameter value, and not to the method being defined. It is as if the compiler implicitly added a this. to Math.abs(this.value).

Adding val or var (or private ... variants) to the constructor parameter doesn’t seem to help.

So, my question is: can I define a property with the same name as a constructor parameter, but maybe a different value? If so, how? If not, why?

Thanks!

  • 1 1 Answer
  • 1 View
  • 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-17T00:48:47+00:00Added an answer on May 17, 2026 at 12:48 am

    No, you can’t. In Scala, constructor parameters are properties, so it makes no sense to redefine them.

    The solution, naturally, is to use another name:

    class NonNegativeDouble(initValue: Double) {
      val value = Math.abs(initValue)
    }
    

    Used like this, initValue won’t be part of the instances created. However, if you use it in a def or a pattern matching declaration, then it becomes a part of every instance of the class.

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

Sidebar

Related Questions

Consider the following Java class: public class Foo { public static void doStuff() {
Consider the following snippet: import java.util.*; public class EqualsOverload { public static void main(String[]
Consider the following class: public class Event<T> { public delegate void Handler<t>(t msg); private
Consider the following class: public class Score { private static readonly Guid _relationId =
Consider the following class class A{ public void init(){ //do this first; } public
Consider the following class: public class MyIntSet { private List<int> _list = new List<int>();
Consider the following class declaration: #include classB.h class A { private: B *prop; public:
Consider the following interface in Java: public interface I { public final String KEY
Consider the following: class MyClass { public: int operator ()(int a, int b); };
Consider the following class hierarchy: public class Foo { public string Name { get;

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.