Revision as of 21:19, 12 November 2006 edit84.221.4.28 (talk) Constructors are NOT methods← Previous edit | Revision as of 22:16, 12 November 2006 edit undo84.221.4.28 (talk) →[]Next edit → | ||
Line 12: | Line 12: | ||
*Constructors can't be final | *Constructors can't be final | ||
*Constructors can't be abstract | *Constructors can't be abstract | ||
*Constructors can't be static | |||
==== Example ==== | ==== Example ==== |
Revision as of 22:16, 12 November 2006
In object-oriented programming, a constructor (sometimes shorted to ctor) in a class is a special block of statements called when an object is declared or explicitally constructed trough the new keyword, depending by the language. A constructor is similar to class methods, but it is not a special kind of method: it never has a return type, it's not inherited, has different rules for modifiers. Constructors are special block of statements that are called automatically upon the creation of an object (instance of a class). They are often distinguished by having the same name as the class. Its main purpose is to pre-define the object's data members and to establish the invariant of the class, failing if the invariant isn't valid. A properly written constructor will leave the object in a 'valid' state.
Java
Some of the differences between Java methods and constructors:
- Constructors never have a return type.
- Constructors can't be overridden, nor they are inherited
- Constructors shouldn't call other overridable methods
- Constructors can't be synchronized
- Constructors are always executed by the same thread
- Constructors can't be final
- Constructors can't be abstract
- Constructors can't be static
Example
public class Example { //declaration of instance variable(s). protected int data; //definition of the constructor. public Example() { data = 1; } //overloading a constructor public Example(int input) { data = input; } }
REALbasic
Example
Constructors in REALbasic can be in one of two forms. Each form uses a regular method declaration with a special name (and no return value). The older form uses the same name as the Class itself, and the newer form uses the name "Constructor." The newer form is the preferred one because it makes refactoring your class easier.
Class Foobar // Old form Sub Foobar( someParam as String ) End Sub // New form Sub Constructor() End Sub End Class
C#
Example
class myClass { private int mA; private string mB;
public myClass(int a, string b) { mA = a; mB = b; } }
Constructors Simplified (with pseudocode)
Constructors are always part of the implementation of classes. A class (in programming) refers to a specfication of the general characteristics of the set of objects that are members of the class rather than the specific characteristics of any object at all. A simple analogy follows. Consider the set (or class, using its generic meaning) of students at some school. Thus we have
class Student { // refers to the class of students // ... more omitted ... }
However, the class Student
is just a generic prototype of what a student should be. To use it, the programmer creates each student as an object or instance of the class. This object is a real quantity of data in memory whose size, layout, characteristics, and (to some extent) behavior are determined by the class definition. The usual way of creating objects is to call a constructor (classes may in general have many independent constructors). For example,
class Student { Student (String studentName, String sex, String Address, int ID) { // ... storage of input data and other internal fields here ... } // ... more omitted ... } // elsewhere: Student john = Student("John Doe", "male", "under the sea", 42);
It is up to the class to decide what to do with the information provided (storing it within the new object is very common, but it might be stored in an entirely different format, passed to other constructors or functions and then forgotten, or even disregarded altogether). The constructed object is persistent, and (depending on the class) may be augmented, examined, or modified in ways related or not to the constructor:
print(john.studentName); // output "John Doe", the internal data print(john.getFirstName()); // object calculates just "John", which is printed john.addNickName("Johnny"); // object remembers this
Here it is assumed that the class provides the studentName
variable and the getFirstName()
and addNickName()
functions, and that the variable is simply assigned the value of the first argument to the constructor. Note that this code does not explicitly mention the class Student
; it is known that the variable john
is an instance of the class, so the elements of the variable are understood to be members of the same class. This implicit specification is a powerful mnemonic, although it is not fundamentally more powerful than function overloading.
See also
References
- Bjarne Stroustrup: The C++ Programming Language, Addison-Wesley, ISBN 0-201-70073-5
- James Gosling: The Java Programming Language, Addison-Wesley, ISBN 0-321-34980-6