Constructor Method
A Constructor Method is an OO Method that creates a new OO Object instance.
- AKA: Constructor, ctor, Constructor Subroutine, Class Constructor.
- Context:
- It can (typically) accept method parameters to set any class variables with default values.
- See: Java Method, Java Constructor Method.
References
2011
- http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29
- In object-oriented programming, a constructor (sometimes shortened to ctor) in a class is a special type of subroutine called at the creation of an object. It prepares the new object for use, often accepting parameters which the constructor uses to set any member variables required when the object is first created. It is called a constructor because it constructs the values of data members of the class.
A constructor resembles a instance method, but it differs from a method in that it never has an explicit return-type, it is not inherited (though many languages provide access to the superclass's constructor, for example through the
super
keyword in Java), and it usually has different rules for scope modifiers. Constructors are often distinguished by having the same name as the declaring class. They have the task of initializing the object's data members and of establishing the invariant of the class, failing if the invariant isn't valid. A properly written constructor will leave the object in a valid state. Immutable objects must be initialized in a constructor.Programmers can also use the term constructor to denote one of the tags that wraps data in an algebraic data type. This is a different usage than in this article.[dubious ] For more information, see algebraic data type.
Most languages allow overloading the constructor in that there can be more than one constructor for a class, each having different parameters. Some languages take consideration of some special types of constructors:
- In object-oriented programming, a constructor (sometimes shortened to ctor) in a class is a special type of subroutine called at the creation of an object. It prepares the new object for use, often accepting parameters which the constructor uses to set any member variables required when the object is first created. It is called a constructor because it constructs the values of data members of the class.
2004
- http://java.sun.com/developer/onlineTraining/Programming/BasicJava1/prog.html#const
- QUOTE:Classes have a special method called a constructor that is called when a class instance is created. The class constructor always has the same name as the class and no return type. The LessonTwoD program converts the LessonTwoB program to use a constructor to initialize the text string. Note: If you do not write your own constructor, the compiler adds an empty constructor, which calls the no-arguments constructor of its parent class. The empty constructor is called the default constructor. The default constructor initializes all non-initialized fields and variables to zero.