views:

290

answers:

6

Why constructor is not considered as member of a class ?
Is there any specific reason ?

Thanks and regards.

+8  A: 

Members are inherited to subclasses. Constructors must not be inherited, so they are not considered to be members.

Constructors are not inherited, because their task is to initialize attributes of their specific class. Any subclass must initialize its additional attributes, and for this task it needs an own constructor that knows about the additional attributes.

Also, each constructor must call one of its superclass constructors directly or indirectly as its first action, to give the superclass a change for initialization.

Christian Semrau
Just to clarify, this answer applies to the Java side of the question. In C#, constructors are class members.
Scott Dorman
+1  A: 

"Understanding constructors" : http://www.javaworld.com/jw-10-2000/jw-1013-constructors.html

zaf
+6  A: 

In C++, constructors certainly are considered to be members of a class - the C++ Standard describes them in a section headed "Special Member Functions".

anon
+2  A: 

As i see it,constructor is not a method but a life-cycle hook provided by Java (another life-cycle hook is finalize). This gives you a chance to do things which would be pre-requisite before any method could be called on the object.So there is clear distinction between methods and constructor, hence even the Language specs has it but the intent is not totally clear from the specs.

+8  A: 

I reject the premise of the question. A constructor is a member of a class or struct in C#.

I refer you to section 3.4.4 ("Class members") of the C# specification, which enumerates the members of a class:

A class declaration may contain declarations of constants, fields, methods, properties, events, indexers, operators, instance constructors, destructors, static constructors and types.

Clearly constructors are members of a class. Why do you believe that a constructor is not a member? Who told you that lie?

Eric Lippert
I agree, as there are cases in C# where the constructor is not even called (such as during DataContract serialization.) The constructor is given special treatment by the language and runtime, but it's still essentially a method. In particular, a constructor on a struct is called with an IL `call` OpCode. I've never tried doing that with IL on a reference type, though; I'm guessing the CLR would complain.
Dan Bryant
Just tried to Call the ctor on a constructed reference type and received a VerificationException "Operation could destabilize the runtime", so a ctor is a member, but definitely given special treatment.
Dan Bryant
A: 

As Eric points out, in C# constructors are considered class members. In fact, it is simply a class method with additional metadata used by the compiler and runtime so the constructor method is invoked during object creation.

Although it doesn't say why, according to the Java Language Specification, Second Edition constructors, static initializers, and instance initializers are not members.

Scott Dorman