What is constructor...any help?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is constructor...any help??

14th Oct 2017, 4:28 PM
Ritu raj
Ritu raj - avatar
2 Answers
+ 19
Constructors Constructors are special methods invoked when an object is created and are used to initialize them. A constructor can be used to provide initial values for object attributes. - A constructor name must be same as its class name. - A constructor must have no explicit return type. //Example of a constructor ::: public class Vehicle { private String color; Vehicle() { color = "Red"; } } /* The Vehicle() method is the constructor of our class, so whenever an object of that class is created, the color attribute will be set to "Red". A constructor can also take parameters to initialize attributes. */
14th Oct 2017, 4:35 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 1
😊👍
14th Oct 2017, 4:50 PM
Ritu raj
Ritu raj - avatar