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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:01:23+00:00 2026-06-13T02:01:23+00:00

Can I pass arguments to Scala class constructor that are not stored into class

  • 0

Can I pass arguments to Scala class constructor that are not stored into class itself?
I want to achieve functionality which in Java could be written as follows:

class A {
    private final SomethingElse y;
    public A(Something x) {
          y = x.derive(this);
    }
}

I.e. class constructor takes parameter that is later transformed to another value using reference to this. The parameter is forgotten after constructor returns.

In Scala I can do:

class A(x: Something) {
    val y = x.derive(this)
}

But it means that x is stored in the class, which I want to avoid. Since x.derive method uses reference to this, I can not make the transformation in companion object.

  • 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-13T02:01:25+00:00Added an answer on June 13, 2026 at 2:01 am

    But it means that x is stored in the class, which I want to avoid.

    If you don’t reference constructor argument anywhere except the constructor itself, field won’t be created. If you reference x e.g. in toString(), Scala will automatically create and assign private val for you.

    Use javap -c -private A to verify what kind of fields are actually created.

    BTW you pass this inside a constructor, which means a.derive() gets a reference to possibly non-initialized instance of A. Be careful!

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

Sidebar

Related Questions

You can pass special strings into jQuery's Datepicker class setDate() method like +7 which
How can I pass a arbitrary number of arguments to the class constructor using
Is there any way that I can pass arguments in selector? example: I have
In Visual Studio Debug part, we can pass arguments. I want to know how
How can I pass back some arguments from a window that is closed? For
I noticed today that I can't use * to pass width or precision arguments
People says that libraries shouldn't have configuration files. I can't pass arguments to my
I know that when you're first instantiating a fragment you can pass arguments using
I want to know how can I pass arguments from the delegate didFinishLaunchingWithOptions to
How can I pass arguments to a function that is assigned to variable. For

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.