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

The Archive Base Latest Questions

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

I need a builder library that can be called from Scala and Java. Easy

  • 0

I need a builder library that can be called from Scala and Java. Easy enough in Scala using default, named parameters. But how do I call this code from Java? See below. Or perhaps I should go with a fluent API that is more common to both languages?

Scala:

case class Person(gender:Gender.Value, firstName:String, lastName:String){
  def fullName = lastName+", "+firstName
  override def toString = firstName+","+lastName+","+gender
}
case class PersonBob(
  gender:Gender = GenderBob().build,
  firstName:String =  null,
  lastName:String = null) {
  def build = Person(
    gender,
    if(firstName == null) NameBob(if(gender==Gender.Male) engMaleNames 
      else engFemaleNames).build else firstName,
    if(lastName==null) NameBob(engLastNames).build 
      else lastName
    )
  }

Usage:

val p1 = PersonBob().build
val p2 = PersonBob(lastName = "Jones").build

Java Usage:

Person p1 = ?
Person p2 = ?
  • 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-23T07:13:22+00:00Added an answer on May 23, 2026 at 7:13 am

    default arguments are not interoperable between scala and java, as mentioned in the very last statement from http://www.scala-lang.org/node/2075.

    to determine how to use your above code from java, perhaps javap can help. let’s take a smaller example than what you have posted. for instance

    case class PersonBob(a: String = "aa", b: String = null)
    

    compiling this using scalac, and then running javap on the produced class file (without the .class extension), we get

    public class PersonBob extends java.lang.Object implements scala.ScalaObject,scala.Product,scala.Serializable {
        public static final java.lang.String apply$default$2();
        public static final java.lang.String apply$default$1();
        public static final java.lang.String init$default$2();
        public static final java.lang.String init$default$1();
        public static final scala.Function1 tupled();
        public static final scala.Function1 curry();
        public static final scala.Function1 curried();
        public scala.collection.Iterator productIterator();
        public scala.collection.Iterator productElements();
        public java.lang.String a();
        public java.lang.String b();
        public PersonBob copy(java.lang.String, java.lang.String);
        public java.lang.String copy$default$2();
        public java.lang.String copy$default$1();
        public int hashCode();
        public java.lang.String toString();
        public boolean equals(java.lang.Object);
        public java.lang.String productPrefix();
        public int productArity();
        public java.lang.Object productElement(int);
        public boolean canEqual(java.lang.Object);
        public PersonBob(java.lang.String, java.lang.String);
    }
    

    we can see that the only constructor we have is

    public PersonBob(java.lang.String, java.lang.String);
    

    so in your case, your java code would look something like

    Person p1 = new PersonBob(new GenderBob().build(), null, null)
    Person p2 = new PersonBob(new GenderBob().build(), null, "Jones")
    

    assuming the GenderBob class takes no parameters in its constructor. as for how fluent this in in Java, i suppose ultimately it is a matter of taste, but in my humble opinion i think the Java version could be less verbose for a builder

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

Sidebar

Related Questions

I am using Flash Builder 4 and need a VideoDisplay component that works with
I need to pick a standard container (JPanel?) in Swing that I can use
Can anyone recommend a GUI builder tool for creating DHTML web apps using AJAX
I'm looking for a good charting library for Java. It can be open source
I need to call a dll function written in Borland C++ Builder 6.0 from
Need a function that takes a character as a parameter and returns true if
Need to an expression that returns only things with an I followed by either
Need a function like: function isGoogleURL(url) { ... } that returns true iff URL
We need to produce a fairly complex dynamic query builder for retrieving reports on
I have refactored some UIView sub-classes into a static library. However, when using Interface

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.