0

Why are default parameters not supported in java language?

For example for call a constructor with 3 parameters in another type constructors with 2 and 1 and 0 parameters, we have to pass all parameters manually! They(parameters) aren't optional like other languages (set automatically default parameters for each constructor that have less than 3 parameters), why?

like this in C# code:

static void Method(int value = 1, string name = "Perl")

and if we can do something better this:

Test(String name,int code,int age)
{
     setTest(name,code,age);
} 
Test(String name,int code)
{
     this(name,code,0);
}
Test(String name)
{
     this(name,0,0);
}

So, we can use final keyword too i know it! If default parameters are not supported, is there anything better or not?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Mohammadreza Khatami
  • 1,444
  • 2
  • 13
  • 27
  • 2
    Java has its own syntax that distinguishes Java from other languages. Language improvements are discussed and implemented in JCP. This feature has not been considered a valid improvement. – Nicola Ferraro Dec 26 '14 at 15:01
  • 1
    possible duplicate of [Technical reason for no default parameters in Java](http://stackoverflow.com/questions/4284019/technical-reason-for-no-default-parameters-in-java) – Tamil Maran Dec 26 '14 at 15:16
  • i was thinking it's better!,but i was wrong,maybe they have important reason for that and each language have own style's and facilities , thank you – Mohammadreza Khatami Dec 26 '14 at 15:18
  • my question was repetitive but i get better answers of before question!thanks – Mohammadreza Khatami Dec 26 '14 at 16:19

3 Answers3

2

To deal with the Object Creation, you can use the Builder Design Pattern as propose this post. This way, you can have default values for some fields.

Community
  • 1
  • 1
ocuenca
  • 38,548
  • 11
  • 89
  • 102
0

There isn't anything better than that, sorry. If you really want to know why default parameters are not supported in Java, you probably should ask Oracle. Or better, make a proposition to add it for the next version of Java.

jlh
  • 4,349
  • 40
  • 45
0

It most likely has to do with a design decision. Java designers may have felt that default parameters, which are allowed in C/C++, could cause subtle bugs. There are many areas of the language where they did things like this where they forced developers to be explicit in their code to reduce this sort of problem.

user2980252
  • 69
  • 1
  • 7