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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:14:10+00:00 2026-05-27T10:14:10+00:00

Following is a Scala class with constructors. My questions are marked with **** class

  • 0

Following is a Scala class with constructors. My questions are marked with ****

class Constructors( a:Int, b:Int ) {

def this() = 
{
  this(4,5)
  val s : String = "I want to dance after calling constructor"
  //**** Constructors does not take parameters error? What is this compile error?
  this(4,5)

}

def this(a:Int, b:Int, c:Int) =
{ 
  //called constructor's definition must precede calling constructor's definition
  this(5)
}

def this(d:Int) 
// **** no equal to works? def this(d:Int) = 
//that means you can have a constructor procedure and not a function
{
  this()

}

//A private constructor
private def this(a:String) = this(1)

//**** What does this mean?
private[this] def this(a:Boolean) = this("true")

//Constructors does not return anything, not even Unit (read void)
def this(a:Double):Unit = this(10,20,30)

}

Could you please answer my questions in the **** above? For example Constructors does not take parameters error? What is this compile error?

  • 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-27T10:14:11+00:00Added an answer on May 27, 2026 at 10:14 am

    Ans 1:

    scala> class Boo(a: Int) {
         |   def this() = { this(3); println("lol"); this(3) }
         |   def apply(n: Int) = { println("apply with " + n) }
         | }
    defined class Boo
    
    scala> new Boo()
    lol
    apply with 3
    res0: Boo = Boo@fdd15b
    

    First this(3) is a delegation to primary constructor. The second this(3) invokes this object’s apply method i.e. expands to this.apply(3). Observe the above example.

    Ans 2:

    = is optional in constructor definitions as they don’t really return anything. They have different semantics from regular methods.

    Ans 3:

    private[this] is called object-private access modifier. An object cannot access other object’s private[this] fields even though they both belong to the same class. Thus it’s stricter than private. Observe the error below:

    scala> class Boo(private val a: Int, private[this] val b: Int) {
         |   def foo() {
         |     println((this.a, this.b))
         |   }
         | }
    defined class Boo
    
    scala> new Boo(2, 3).foo()
    (2,3)
    
    scala> class Boo(private val a: Int, private[this] val b: Int) {
         |   def foo(that: Boo) {
         |     println((this.a, this.b))
         |     println((that.a, that.b))
         |   }
         | }
    <console>:17: error: value b is not a member of Boo
               println((that.a, that.b))
                                     ^
    

    Ans 4:

    Same as Ans 2.

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

Sidebar

Related Questions

Considering the following Scala snippet: case class Foo(v1: String, v2: Int, v3: Any) def
Consider the following Scala case class: case class WideLoad(a: String, b: Int, c: Float,
I have the following Scala class: case class Person(firstName: String, lastName: String, age: Int)
Have a look at the following Scala example: class A { def foo(x: Int):
Suppose I have the following Scala code: class Foo(a: Int) class Bar(b: Buffer[Int]) extends
Consider the following Scala code: package scala_java object MyScala { def setFunc(func: Int =>
Given the following Scala definitions abstract class C { type T1 <: { def
Following on from this question , can someone explain the following in Scala: class
Given the following Scala List: val l = List(List(a1, b1, c1), List(a2, b2, c2),
I am generating the scala AST using the following code: val setting = new

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.