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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:32:09+00:00 2026-05-23T03:32:09+00:00

Most of the time, a constructor for a class does nothing more than take

  • 0

Most of the time, a constructor for a class does nothing more than take its argument values and use them to set instance variables:

// Java
public class MyClass {
   private int id;

   public MyClass(int id) {
      this.id = id;
   }
}

So I understand the efficiency of Scala’s default constructor syntax… simply declaring a list of variables in parentheses beside the class name:

// Scala
class MyClass(id: int) {
}

However, what about those circumstances where you need a constructor to actually DO STUFF, apart from simply plugging arguments into instance variables?

// Java
public class MyClass {
   private String JDBC_URL = null;
   private String JDBC_USER = null;
   private String JDBC_PASSWORD = null;

   public MyClass(String propertiesFilename) {
      // Open a properties file, parse it, and use it to set instance variables.
      // Log an error if the properties file is missing or can't be parsed.
      // ...
   }
}

How does this work in Scala? I can try to define an implementation for this constructor like so:

// Scala
class MyClass(propertiesFilename: String) {
  def this(propertiesFilename: String) {
    // parse the file, etc
  }
}

… but I get a compilation error, complaining that the constructor is defined twice.

I could avoid this conflict by having a no-arg default constructor, and then declaring the above as an overloaded secondary constructor. However, what about situations in which you really DO need “one-and-only-one” constructor, and you need it to do stuff?

  • 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-23T03:32:10+00:00Added an answer on May 23, 2026 at 3:32 am

    You can perform these actions simply in the class body.

    Class Foo(filename: String) {
        val index =  {
             val stream = openFile(filename)
             readLines(stream)
             ...
             someValue
         }
         println(“initialized...“) 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Most of time we represent concepts which can never be less than 0. For
When I use Build->Publish Web Site in Visual Studio 2008, most of the time
For a particular segment of Java code, I'd like to measure: Execution time (most
Most of the time, I've seen release management handled as a defined process with
Spending most of my time in Visual Studio and using all the IDE tools,
I would like to hide the UISearchBar most of the time and only call
As I put together each asp.net page It's clear that most of the time
I've got 2 monitors, and most of the time I've got some reference material
I've got a php script. Most of the time the script returns html, which
When running PHP in CLI mode, most of the time (not always), the script

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.