4

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

Thanks and regards.

Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
  • Hey that wasn't a homework ! That was one of the basic Q's that any java programmer would come across with. – Saurabh Gokhale May 08 '10 at 10:07
  • Haha, I was just about to add a homework tag ;) – Thorarin May 08 '10 at 10:10
  • 7
    Homework tags should only be added by the OP - others have no means of knowing if a question is homework or not. –  May 08 '10 at 10:16
  • 2
    Not homework? Why is this guy asking the exact question with the exact wording on the exact same day? http://stackoverflow.com/questions/2796790/programming-languages-and-constructors – brydgesk May 09 '10 at 06:18

6 Answers6

9

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
  • 647,829
  • 179
  • 1,238
  • 2,067
  • 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 May 08 '10 at 15:30
  • 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 May 08 '10 at 16:03
8

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
  • 8,913
  • 2
  • 32
  • 39
7

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

2

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.

Santosh Gokak
  • 3,393
  • 3
  • 22
  • 24
1

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

zaf
  • 22,776
  • 12
  • 65
  • 95
0

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.

Community
  • 1
  • 1
Scott Dorman
  • 42,236
  • 12
  • 79
  • 110
  • It's the job of a specification to specify things, not really to discuss why (which I believe you'll so more so in the next edition). Anyway I think the linked sentence about not being inherited says enough. – Tom Hawtin - tackline Feb 14 '11 at 04:14