DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Named Parameters in Java – an Alternative

Named Parameters in Java – an Alternative

Jens Schauder user avatar by
Jens Schauder
·
Aug. 15, 12 · Interview
Like (0)
Save
Tweet
Share
4.30K Views

Join the DZone community and get the full member experience.

Join For Free

In the last installment of this blog I wrote about a way to fake Named Parameters in Java. As so often there are many ways to do this, so here comes another one.

Instead of static methods producing value objects for each parameter you can use a single parameter object that happens to be a builder:

The method signature would look like this:

void doSomething3(ParameterBuilder pb)

The usage looks like this:

 doSomething3(new ParameterBuilder()
        .name("Alfred E. Neumann")
        .link("http://blog.schauderhaft.de")
        .ultimateAnswer(42)
        .tempFile("c:\\temp\\x.txt")
        .zip(23)
    );

and the builder itself like this:

    public class ParameterBuilder {
     
        private String link;
        private String name;
        private int answer;
        private int zip;
        private String file;
     
        public ParameterBuilder name(String aName) {
            name = aName;
            return this;
        }
     
        public ParameterBuilder link(String aLink) {
            link = aLink;
            return this;
        }
     
        public ParameterBuilder ultimateAnswer(int anAnswer) {
            answer = anAnswer;
            return this;
        }
     
        public ParameterBuilder zip(int aZip) {
            zip = aZip;
            return this;
        }
     
        public ParameterBuilder tempFile(String aFile) {
            file = aFile;
            return this;
        }
     
        // … getters for the fields omitted.
    }

In a sense this is even more similar to named parameters, since you can define default values in the builder, thereby making arguments optional. Also the order of arguments does not matter.

If you want to use the parameter object for more then transfering a bunch of values from one side of a method signature to the other you should consider actually creating an immutable object from you builder (or at least passing it as an implementation of an immutable interface)

 

Java (programming language)

Published at DZone with permission of Jens Schauder, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Kubernetes vs Docker: Differences Explained
  • How and Why You Should Start Automating DevOps
  • How to Quickly Build an Audio Editor With UI
  • How To Use Terraform to Provision an AWS EC2 Instance

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: