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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:36:59+00:00 2026-06-17T23:36:59+00:00

In Scala I need to do something like following code in Java does: public

  • 0

In Scala I need to do something like following code in Java does:

public class A {
  private String text;  
  public A(String text) {
    this.text = text;
  }
}

How can achieve that in Scala?

I know that I can use class A(text: String) { ... }, but this is only a very simplified example and not real case.

I have tried the following and it prints always null:

class A {
  var text: String = null
  def this(text: String) = {
    this()
    this.text = text
  }
  println(text)
}

Thanks for help.

  • 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-17T23:37:00+00:00Added an answer on June 17, 2026 at 11:37 pm

    In Scala classes have only one main constructor, and it’s exactly what you define like this: class A(text: String) { ... }. All other constructors should call it at first and then do their own stuff.

    All expressions in the body of the class are treated as body of the main constructor. So println(text) in you case is in the body of the main constructor. You call this() in your def this(text: String) constructor at first, so println is executed and only then you initialize text field.


    From my point of view, main constructor can satisfy most of the real-life use-cases (especially with default and named arguments). So can you please elaborate on this and explain why you need the second one? Maybe we can find beter solution for the problem you are facing there.


    Update regarding the comment

    If you want to provide a set of optional arguments during construction time, then I recommend you to use Option instead of null values. So you can implement your A class like this:

    class A(val a: Option[String] = None, val b: Option[Int] = None, c: Option[Double] = Some(2.0)) 
    
    new A(b = Some(10))
    

    All fields are now constant and have some default, but you can customize some of them. It’s also possible that some combination of arguments are mutually exclusive, according to some business logic, for example. In this case it’s reasonable to use several constructors. But even better solution can be to create several factory methods in companion object of the class and make constructor private. For example, if users of the class are allowed to provide either a and b or c, then you can write something like this:

    class A private (val a: Option[String] = None, val b: Option[Int] = None, c: Option[Double] = Some(2.0)) 
    
    object A {
        def apply(a: String, b: Int) = new A(Some(a), Some(b))
        def apply(c: Double) = new A(c = Some(c))
    }
    
    A("test", 1)
    A(11.1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following spring pseudo-code wired scala class. @Service class Something { @Value(${some.property})
I need to convert the following java method to scala and am having difficulty
Is there anything in Java that implements something like the following interface MSet<T> extends
Given the following code from a Microsoft example: public class EngineMeasurementCollection : Collection<EngineMeasurement> {
I'd like to disable truncation of string values in the Scala REPL. The following
In Scala 2.10.0-RC1 there's a wonderful feature, the String Interpolation . This works like
Does Scala support something like dynamic properties? Example: val dog = new Dynamic //
I have a Scala/Java dual language project where I need to pass a Scala
How can I create a node in Scala without using literals? What I need
On large-scale Java/.Net Enterprise projects, does every developer need to have all the components/libraries/dependencies

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.