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

  • Home
  • SEARCH
  • 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 6229457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:34:11+00:00 2026-05-24T09:34:11+00:00

I may be having a silly problem here… I can’t seem to figure out

  • 0

I may be having a silly problem here… I can’t seem to figure out how to make a constructor without parameters in Scala. I know I can just write the whole thing in the class body (especially because it’s the only constructor I need), but it doesn’t quite feel right.

What I have:

    class Foo {
        //some init code

        //...
    }

What I’d like (but doesn’t work as it wants me to call another constructor first):

    class Foo {
        // The only constructor
        def this() = { 
            //some init code
        }

        //...
    }
  • 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-24T09:34:12+00:00Added an answer on May 24, 2026 at 9:34 am

    All classes in Scala have a primary constructor and optionally some auxiliary constructors (which must defer to the primary constructor or another auxiliary constructor).

    The issue in your case is that in both cases you’ve defined the primary constructor as taking no arguments – and then in the second case you try to define an auxiliary constructor with the same signature. This doesn’t work, for the same reason that the following wouldn’t compile:

    // Primary constructor takes a String
    class Foo(s: String) {
        // Auxiliary constructor also takes a String?? (compile error)
        def this(a: String) {
            this(a)
        }
    }
    

    This isn’t anything to do with the fact that the constructor is no-args; the following compiles for example:

    class Foo(s: String) {
        // alternative no-arg constructor (with different signature from primary)
        def this() {
            this("Default value from auxiliary constructor")
        }
    }
    

    In particular, in your second example, your comment “the only constructor” is wrong. Auxiliary constructors are always secondary to the primary constructor, and cannot ever be the only constructor.

    FWIW, the first example is the only option open to you, but it looks fine to me. If you’ve just started using Scala I’m sure it will start to feel right soon enough – and it’s important to eschew Java-esque ways of doing things when there are more idiomatic alternatives.

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

Sidebar

Related Questions

This may seem like a silly question, but we are having an issue debugging
This may seem a silly question, but having been pondering it for a few
This may be something common and trivial, but I seem to be having trouble
im having difficulties figuring this out, ive looked at examples here and on the
May I know what is the purpose of having .hgtags? Can I remove it?
This may look a very basic question but I am having a problem accessing
I'm having a couple of issues which may be related, or may not. I
As many of you may already know I have been having issues with another
I'm having a really hard time understanding delegates and object inheritance (if I may
I'm having a real trouble to get accents right, and I believe this may

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.