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

R

Razvan

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
 
T

Tor Iver Wilhelmsen

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top