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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:43:34+00:00 2026-05-11T21:43:34+00:00

I was just reading a Java 7 preview presentation (pdf) and there was a

  • 0

I was just reading a Java 7 preview presentation (pdf) and there was a slide on Chained Invocation. Here is the example used in the slide:

// Construction with setters
DrinkBuilder margarita = new DrinkBuilder();
margarita.add("tequila");
margarita.add("orange liqueur");
margarita.add("lime juice");
margarita.withRocks();
margarita.withSalt();
Drink drink = margarita.drink();

// Construction with chained invocation
Drink margarita = new DrinkBuilder()
    .add("tequila")
    .add("orange liqueur")
    .add("lime juice")
    .withRocks()
    .withSalt()
    .drink();

And I have mixed feelings about this. One shouldn’t chain too many method invocations into one statement. On the other hand, writing margarita.this() and margarita.that() isn’t too convenient either.

Now, I am coming to Java from Delphi world. And in Delphi there is the with language construct. This is cherished by a few and loathed by many (or is it the other way around?). I find with to be more elegant than the idea of chained invocation (which I believe works on the basis of void method returning reference to object on which it has been invoked – and this is the part I don’t like, as void should return nothing).

I would appreciate the with language feature being adopted by Java, so the example code could be written like so:

Drink margarita = null;
with (new DrinkBuilder()) {
    add("tequila");
    add("orange liqueur");
    add("lime juice");
    withRocks();
    withSalt();
    margarita = drink();
}

Am I the only one who would prefer this solution to the chained invocation? Anyone else feels that with could be a useful extension to Java language? (Reminds me of someone’s question about the need of “Java++”…)

  • 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-11T21:43:34+00:00Added an answer on May 11, 2026 at 9:43 pm

    the with statement can be translated in Java using anonymous classes with initializer:

    Drink margarita = new DrinkBuilder() {{
        add(“tequila”);
        add(“orange liqueur”);
        add(“lime juice”);
        withRocks();
        withSalt();
    }}.drink();
    

    the downsides of using this idiom are well documented here.

    Chained Invocation is an alias for Method Chaining. That is well known idiom and works with any version of Java:

    class Chained {
    
        public Chained withFoo() { 
            // ...
            return this;
        }
    
        public Chained withBar() { 
            // ...
            return this;
        }
    }    
    

    a proposal for JDK 7 is allowing of chaining method also for void return type:

    class ChainedJava7 {
    
        public void withFoo() { 
            // ...
        }
    
        public void withBar() { 
            // ...
        }
    }    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 365k
  • Answers 365k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use the data in the /proc filesystem to… May 14, 2026 at 3:57 pm
  • Editorial Team
    Editorial Team added an answer Do you still get the error when you use a… May 14, 2026 at 3:57 pm
  • Editorial Team
    Editorial Team added an answer You can use array_chunk to create a single array comprised… May 14, 2026 at 3:57 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.