Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4189007

A method can be defined with the same name as constuctor of the class.

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.1.6
    • tools
    • generic
    • generic



      Name: diC59631 Date: 11/11/98


      // the class
      public class MyClass {
          // the constructor
          public MyClass() {
              // do something
          }

          // the method with the same name as constructor
          public void MyClass() {
              // do something
          }
      }

      In other program, you can use the class like this:
          // call the constructor
          MyClass myclass = new MyClass();
          // call the method public void MyClass()
          myclass.MyClass();

      The java complier allows this kind of usage, but it
      looks confusing(It is not allowed in C++).

      If you forgot to implement the constructor(with a
      "void" before the method name), the code is also
      allowed. But the default constructor does nothing,
      so you maybe think it strange.

      I think the java complier should check this situation,
      and do not allow the method with the same name as
      the constructor.
      (Review ID: 42467)
      ======================================================================

            tturnidgsunw Todd Turnidge (Inactive)
            dindrigo Daniel Indrigo (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: