can someone please tell me what constructors are??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

can someone please tell me what constructors are???

14th Jan 2017, 1:27 AM
Ivan
Ivan - avatar
4 Answers
+ 6
as a function it is, params of contructor are the same of any function. They can change, they even can be empty, its all up to how its defined. Person p1 = new Person(); for example, this one is empty, so the constructor params don't require anything. Person p1 = new Person("Ivan"); Instead, this one requires a String parammeters, wich is the name, to later be assigned to the atributte class for p1 object.
14th Jan 2017, 1:43 AM
Nahuel
Nahuel - avatar
+ 4
A method from class which is called when you use the new command (creating a new object). Even if you don't create one, there is always the default, which is empty. the constructors are mostly publics and exactly the same name of the class.
14th Jan 2017, 1:41 AM
Nahuel
Nahuel - avatar
+ 1
For example: class Person{ int age; string name; Person(int age, string name){ this.age = age; this.name = name; } } Here, the parameters of the Constructor are age and name inside the parentheses of the Constructor, Person(int age, string name) - when constructing a new object of the Person class you pass concrete arguments for those parameters like this: Person p1 = new Person(34, Michael);
14th Jan 2017, 9:42 AM
everSin berserch
everSin berserch - avatar
0
Thanks, but what are parameters of a constructor.
14th Jan 2017, 1:41 AM
Ivan
Ivan - avatar