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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:48:17+00:00 2026-05-20T18:48:17+00:00

In my one class I have many constructors like this.. public MyData(int position,String songName,String

  • 0

In my one class I have many constructors like this..

public MyData(int position,String songName,String duration, boolean e) {

    //initialization of above variable like int, string,string and boolean

}

public MyData(String songName, String artistName, String duration,String downloadPath, String songSize, String albumName,String url,String trackId, boolean e) 
{
 //initialization of above variable like String,String,String,String,String,String,String,String and boolean

}

and some more like above.
Now the calling time, I’m calling that constructor only that I require data. but I don’t think my flow is good so I need some help to reduce my code as well as creation of good flow.
If anybody have a good flow to achieve this, then please share.

Thanks in advance.

  • 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-20T18:48:17+00:00Added an answer on May 20, 2026 at 6:48 pm

    Assuming you’re effectively applying defaults, usually the best approach is to have one “full” constructor and make the others call it. For example:

    public Foo(String name)
    {
        // Default the description to null
        this(name, null);
    }
    
    public Foo(String name, String description)
    {
        this.name = name;
        this.description = description;
    }
    

    You still end up with quite a lot of cruft in terms of overloaded constructors, but at least each of those “extra” constructors contains no actual code – just a call to another constructor. If possible, chain the constructors together so that the default for any particular value is only specified in one place – or use a constant. That way you get consistency.

    Another option is to use a “parameter object” following the builder pattern – create another class whose sole purpose is to hold the data for the constructor parameters. This should be mutable, with setters for all of the different values. Often it’s useful to make the setters return the builder, so you can use:

    FooParameters parameters = new FooParameters()
        .setName("some name")
        .setDescription("some description");
    
    // Either a constructor call at the end, or give FooParameters
    // a build() or create() method
    Foo foo = new Foo(parameters);
    

    This is particularly useful if the main type you’re constructing is an immutable type – it means you can apply conditional logic in the calling code to set some parameters but not others. The Java framework itself uses this approach in ProcessBuilder, although personally I’m not keen on the way it overloads method names to either return a value or set a value based on whether you provide an argument 🙁

    Note the comment above the constructor call in the final snippet – if your helper class is only ever helpful for creating objects of a single type, you can give it an extra method (build, create, start, whatever is most appropriate) to take the place of the constructor call. This allows you to build the whole final object in a fluent way.

    One option in the Java implementation of the builder pattern is to use a nested type, e.g.

    Foo foo = new Foo.Builder().setName(...).setDescription(...).build();
    

    That avoids polluting your package with another class which is only useful for building instances of Foo.

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

Sidebar

Related Questions

I've got a class with many string arrays. I'd like to have one generic
I have a static Command class like this (but with many more commands): class
Say I have a class like this: class Person { private $value; public function
I have only one class with many instances. Every instance is observer of couple
I have one abstract class and many child classes. In child classes are from
I have two models related one-to-many: a Post and a Comment : class Post(models.Model):
I have a public class with 2 constructors: The default (without parameters), which is
I have data from a CD or Record currently setup like this: class Release
Assume I have a Class Foo which has many internal variables, only one constructor
In one class I have a library project with a file class1 with namespace

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.