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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:23:13+00:00 2026-05-23T18:23:13+00:00

I’m still trying to wrap my head around Scala constructors public class MyClass {

  • 0

I’m still trying to wrap my head around Scala constructors

public class MyClass {
    private String myString = null;

    public MyClass() {
        myString = "hello";
    }
} 
  • 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-23T18:23:14+00:00Added an answer on May 23, 2026 at 6:23 pm

    In Scala you would just do

    class MyClass {
      private var myString = "hello"
    }
    

    This doesn’t look like the same thing, does it? But let’s look at the bytecode for what the Java code actually produces (let’s call that class JavaConstructor):

    public JavaConstructor();
      Code:
       0:   aload_0
       1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
       4:   aload_0
       5:   aconst_null
       6:   putfield    #2; //Field myString:Ljava/lang/String;
       9:   aload_0
       10:  ldc #3; //String hello
       12:  putfield    #2; //Field myString:Ljava/lang/String;
       15:  return
    

    Java has effectively copied the private String myString = null; into the constructor (note lines 5 and 6 where null is created and stored). So that whole thing about setting myString to null was a complete waste of effort; you load it and then immediately overwrite it.

    Now if we look at the corresponding Scala:

    public ScalaConstructor();
      Code:
       0:   aload_0
       1:   invokespecial   #18; //Method java/lang/Object."<init>":()V
       4:   aload_0
       5:   ldc #20; //String hello
       7:   putfield    #10; //Field myString:Ljava/lang/String;
       10:  return
    }
    

    we see it’s as efficient as one would hope (and reflects the code).

    Then the question is: why might you want to do it the way you did in Java? Well, maybe you have multiple constructors and some of them will set myString and some won’t! Setting it to null is a way to remind yourself that you had better initialize it before you use it.

    But Scala won’t let you do that. Scala really only allows one constructor; the others are just aliases that call that one constructor. You can make it private and load it up with lots of parameters if you need to, but the point is that having multiple constructors some of which set critical data and some of which do not is actually a rather failure-prone process. Better to do it just once, and write something like

    private var myString = if (someParameter) "hello" else null
    

    if you really need to do that. Then again, maybe you only made it a var because you were trying to set it differently in different constructors. With only one constructor, maybe there’s no need for that:

    private val myString = if (someParameter) "hello" else null
    

    But maybe now that it’s set correctly, we don’t really need it to be private.

    val myString = if (someParameter) "hello" else null
    

    and maybe if it often isn’t set, we should use an option instead:

    val myString = if (someParameter) Some("hello") else None
    

    and then you’d have something that looks more like idiomatic Scala.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And

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.