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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:55:37+00:00 2026-06-13T08:55:37+00:00

I have a Scala class: class Foo(val x:String = default X, val y:String =

  • 0

I have a Scala class:

class Foo(val x:String = "default X", val y:String = "default Y" ) 

I want to call it from Java, but using the default parameters

Passing null doesn’t work (it assigns null, as expected)

new Foo(null,null); //both are instantiated as null

This trick did work for me, but it’s ugly, and I wonder if there is a better way:

Scala

class Foo(val x:String = "default X", val y:String = "default Y" ) {
  def this(x:Object) = this()
}

Java

new Foo(null); //no matter what I pass it should work

However I would like to get rid of the constructor overload trick, and use a 0 param constructor

Is that possible?

  • 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-13T08:55:38+00:00Added an answer on June 13, 2026 at 8:55 am

    It seems, there is no such way: https://issues.scala-lang.org/browse/SI-4278

    Issue: default no-args constructor should be generated for classes with all-optional arguments
    …

    Lukas Rytz: in respect of language uniformity we decided not to fix this one – since it’s a problem of interoperability with frameworks, we think it should not be fixed at the language level.

    workarounds: repeat a default, or abstract over one, or put one default int the zero-argument constructor

    Then Lukas proposes the same solution as you found:

    class C(a: A = aDefault, b: B = C.bDefault) {
      def this() { this(b = C.bDefault) }
    }
    object C { def bDefault = ... }
    
    // OR
    
    class C(a: A = aDefault, b: B) {
      def this() { this(b = bDefault) }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Scala, if we have class Foo(bar:String) We can create a new object but
I have a scala class: class Foo { def this(st: String) { var baz
Does Scala have a version of Rubys' each_slice from the Array class?
I have a class that I would like to use in a scala.collection.mutable.PriorityQueue, but
In Scala, if I have a simple class as follows: val calc = actor
I have a number of classes that look like this: class Foo(val:BasicData) extends Bar(val)
Suppose I have the following Scala code: class Foo(a: Int) class Bar(b: Buffer[Int]) extends
I have a type like this sealed class Foo[A](val value: A) object Foo {
I am using JavaFX 2 with Scala. I have the class Application extends javafx.application.Application
Suppose I have a the following in Scala object Foo { var functions: List[String

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.