Constructors are having a special signature, different from ordinary member functions ?

Discussion in 'Java' started by Razvan, Sep 30, 2004.

  1. Razvan

    Razvan Guest

    Hi !



    Take a look at the following code:

    public class CDummy
    {
    public static void main(String args[])
    {
    System.out.println("CDummy.");

    CDummy dmy = new CDummy();
    }

    CDummy() {System.out.println("CDummy()");}

    void CDummy() {System.out.println("void CDummy()");}
    }


    The constructor "CDummy()" and the member function "void CDummy()"
    differ only by the return type. Normally, this is disallowed but since
    constructors can only appear after a 'new' operator there is no
    ambiguity, so this is allowed !!!

    Well.. no question here. I hope I didn't messed up the explanation:))



    Regards,
    Razvan
     
    Razvan, Sep 30, 2004
    #1
    1. Advertising

  2. (Razvan) writes:

    > The constructor "CDummy()" and the member function "void CDummy()"
    > differ only by the return type. Normally, this is disallowed but since
    > constructors can only appear after a 'new' operator there is no
    > ambiguity, so this is allowed !!!


    Actually, the reason it's allowed is because there's no conflict:
    Constructors are turned into methods called "<init>" by the compiler,
    so there is no collision with "normal" methods of the same name.
     
    Tor Iver Wilhelmsen, Sep 30, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JJBW
    Replies:
    1
    Views:
    10,530
    Joerg Jooss
    Apr 24, 2004
  2. prabhu
    Replies:
    3
    Views:
    790
    Roger Leigh
    Jan 27, 2004
  3. Replies:
    4
    Views:
    285
    Victor Bazarov
    Jul 20, 2006
  4. laikon
    Replies:
    13
    Views:
    504
    Jerry Coffin
    Apr 3, 2008
  5. Gustavo Narea
    Replies:
    14
    Views:
    889
    Gustavo Narea
    Feb 16, 2009
Loading...

Share This Page