0

Constructors

Okay this is something I don't complety understand. can somebody dump it down. and give me a few examples?

31st Dec 2016, 1:06 AM
Juan Madrigal
Juan Madrigal - avatar
1 Answer
+ 1
Hi, Constructor is a special type of method that is used to initialize object. This will be the starting method that will be getting called during object creation. There are 2 types of constructor Default & Parameterized constructor The basic idea of creating a constructor is to initialize a value during initializing of the object. Take a example for both, CLASS :- Student VARIABLES :- rollNo (int), studName (String); Default Constructor: A constructor that have no parameter is known as default constructor. This can be defined explicitly or if not defined, compiler assigns default. Default constructor provides the default values to the object like 0, null etc. depending on the type. If you execute and look for the value of the variable, the output will be 0 and null which is default. Parameterized Constructor: A constructor that have parameters is known as parameterized constructor. Now let’s define a constructor to initialize a value for the fields, that will be assigned during object creation Student (int id, String name){ rollNo = id; studName = name; } When a object is getting created the value of the id and name can be passed which will be assigned to the object. Hope this was helpful :-)
31st Dec 2016, 3:49 AM
Yukeshkumar N
Yukeshkumar N - avatar