Methods With Same Name as Constructor - Why?

后端 未结 7 855
终归单人心
终归单人心 2020-12-01 16:09

Why is the following allowed:

public class Foo {
  public Foo() { ... }
  public void Foo() { ... }
}

Is there ever a valid reason for nami

7条回答
  •  庸人自扰
    2020-12-01 16:38

    the reason is that in constructors we pass the values at the time of the object creation of the class.so thats why the name of the constructor should be the same so that it can take passed values and initialized at the time of the object creation of the class.

提交回复
热议问题