Contrcutor
Hi can any one tell me why we intilized the mostly class level members through contrcutor....?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
SenthilkumarRank #387 · 6.5K reputationPosted Apr 30, 2012, 6:36 AM EDT
The constructor is used to initialize the member variables in the class. Because every class will have the default constructor.
When we try to assign the values to the member variables out side the class then constructor is prefer method. Even it supports to do constructor overloading.
There is no need of writing a method to assign the values, because it can be assign the values at the time of creating object to the class.
Thats why constructor is preferred.
Krishna GaradRank #191 · 12.8K reputationPosted Apr 30, 2012, 2:56 AM EDT
because cunstructor is the member of the class which executes before starting execution of the other class member means cunstrocture executes when we create the instance of the class and if we need to initilize any another member of the class we can do it in custrocture. This is in case of public cunstrocture but in case of static cunstrocutur we can not say when it will execute but it's fix it will execute before any other member execution starts.
VulpesRank #55 · 29.2K reputationPosted Apr 30, 2012, 3:04 AM EDT