32

Is it possible to use default argument in the method with String. The code is shown below:

public void test(String name="exampleText") {
}

The code above generate error. Is it possible to correct it?

user2864740
  • 57,407
  • 13
  • 129
  • 202
user3455638
  • 559
  • 1
  • 5
  • 16

2 Answers2

61

No, the way you would normally do this is overload the method like so:

public void test()
{
    test("exampleText");
}

public void test(String name)
{

}
MrLore
  • 3,713
  • 2
  • 25
  • 36
16

No, it is not. However, the following is possible:

public void test() {
    test("exampleText");
}
public void test(String name) {
    //logic here
}
nanofarad
  • 38,481
  • 4
  • 83
  • 110