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

  • Home
  • SEARCH
  • 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 192663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:26:41+00:00 2026-05-11T16:26:41+00:00

Following are the two approaches: constructor with all the class properties Pros: I have

  • 0

Following are the two approaches:

  • constructor with all the class properties

Pros: I have to put an exact number of types of parameters so if I make an error the compiler warns me (by the way, is there a way to prevent the problem of having erroneously switched two Integer on the parameter list?)

Cons: if I have lots of properties the instantiation line can become really long and it could span over two or more lines

  • setters and the default empty constructor

Pros: I can clearly see what I’m setting, so if I’m doing something wrong I can pinpoint it as soon as I’m typing it (I can’t make the previuos error of switching two variables of the same type)

Cons: the instantiation of an object with lots of properties could take several lines (don’t know if this is really a con) and if I forget to set a property the compiler doesn’t say anything.

What will you do and why?
Do you know of any light pattern (consider that it should be used everytime an object wth 7+ properties is instantiated) to suggest?
I’m asking this because I tend to dislike large constructors where I can’t figure out fast where is the variable I’m looking for, on the other hand I find the “set all properties” vulnerable to missing some of the properties.

Feel free to argument my assumptions in pros and cons as they are only mine thoughts 🙂

Update – a question I’ve found which is related to this: Building big, immutable objects without using constructors having long parameter lists

  • 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-11T16:26:41+00:00Added an answer on May 11, 2026 at 4:26 pm

    You might look at the Builder pattern advocated by Joshua Bloch, and described in Effective Java. There’s a presentation with the main points at http://developers.sun.com/learning/javaoneonline/2007/pdf/TS-2689.pdf; no doubt you could dig up a better reference.

    Basically, you have another class, probably an inner class, which provides methods named after the properties being set, and which return the original builder so you can chain calls. It makes for quite a readable chunk of code.

    For example, let’s suppose I have a simple Message with a few properties. The client code constructing this could use a builder to prepare a Message as follows:

    Message message = new Message.Builder()
        .sender( new User( ... ) )
        .recipient( new User( ... ) )
        .subject( "Hello, world!" )
        .text( messageText )
        .build();
    

    A fragment of Message.Builder might look similar to the following:

    public class Builder {
    
        private User sender = null;
        // Other properties
    
        public Builder sender( User sender ) {
            this.sender = sender;
            return this;
        }
        // Methods for other properties
    
        public Message build() {
            Message message = new Message();
            message.setSender( sender );
            // Set the other properties
            return message;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following two strings containing regular expressions. How do I coalesce
I have tried the following two statements: SELECT col FROM db.tbl WHERE col (LIKE
I have the following two columns: SELECT b.ip_address AS IP ,b.mask AS MASK FROM
I have a class structure such as following for storing static final constants: public
The following two forms of jQuery selectors seem to do the same thing: $(div
The following two queries each give the same result: SELECT column FROM table GROUP
Take the following two lines of code: for (int i = 0; i <
Consider the following two ways of writing a loop in Java to see if
Consider the following two alternatives: console.log("double"); console.log('single'); The former uses double quotes around the
When the following two lines of code are executed in a bash script, ls

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.