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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:46:03+00:00 2026-05-28T01:46:03+00:00

Using Clojure, how do I create the following object? The object is taken from

  • 0

Using Clojure, how do I create the following object? The object is taken from java code (From Effective Java):

NutritionFacts cocaCola =
new NutritionFacts.Builder(240,8).calories(100).sodium(35).carbohydrate(27).build();

  • 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-28T01:46:04+00:00Added an answer on May 28, 2026 at 1:46 am

    While it’s hard to argue with the concision in the other answers1, .. has somewhat fallen out of favor, replaced by the more versatile ->. Personally I prefer :

    (-> (NutritionFacts$Builder. 240 8) 
        (.calories 100)
        (.sodium 350)  
        (.carbohydrates 27) 
        (.build))
    

    It’s a couple more characters, but you gain two things:

    • Explicitness. I can look at the sodium line (for example) and tell it’s a Java method call, because the . is right there.
    • Flexibility. If I need to, I can chain some non-method call in the middle there (printing it to stdout, say), or at the end of all this feed it in to some other function call.

    Most importantly, every other answer to this question has gotten the classname wrong: Java’s NutritionFacts.Builder is language sugar over the real JVM class named NutritionFacts$Builder, and that class is the one Clojure must refer to (since we are not using javac to compile our code).

    1 I do disagree with the doto suggestion: it works only because this Builder class happens to implement its method-chaining by mutating a single instance and then returning it. doto is great for Java objects that require in-place mutation, but when a class is kind enough to pretend it’s immutable you should really be using the method-chaining version (ie, ->).

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

Sidebar

Related Questions

I am using the following code to dynamically create an INPUT element and assign
So following on from Clojure macro to create a synonym for a function ,
I have a situation, where I need to create a new JavaScript object that
I have a problem with the Enclojure REPL and using clojure modules from it.
I've been using Clojure for a little while and want to create some projects
I am trying to find a Clojure-idiomatic way to initialize a Java object. I
I have recently started out with clojure, and I am using leiningen to create
Consider the following code: private Dictionary<RobotSettings, Trader> createTradersFor(IEnumerable<RobotSettings> settings) { var traderSet = new
I'm using PHPs create_function($args, $code) function to dynamically load a function definition from a
Still working through Programming Collective Intelligence and using Clojure to write the code. I've

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.