Computer Applications
What is a constructor? Why do you need a constructor?
Java Constructors
10 Likes
Answer
A constructor is a member method of a class that is used to initialise the instance variables. It has the same name as that of its defining class and does not have a return type.
A constructor is needed as it instructs the computer to perform the start-up tasks of a new object. These can be like assigning default values to instance variables, aligning all the buttons on a screen when a new window object is created, or moving the cursor to a specific data entry field for the user input.
Answered By
5 Likes
Related Questions
Pick the correct answer.
- A constructor has exactly the same name as its defining class.
- A constructor method does not have a return type.
- A constructor can be invoked only via the new operator.
- All of the above
In constructor overloading, …………… .
- All constructors must have the same name as that of the class.
- All constructors must have the same number of arguments.
- All constructors must have arguments of type String[] args.
- All constructors must have no arguments.
If the constructor is automatically generated by the compiler, why do you need to define your own constructor?
Explain the statement, "you cannot invoke constructors as normal method calls".