Do default constructor called automatically while creating object even If I don't provide any statement in it? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Do default constructor called automatically while creating object even If I don't provide any statement in it?

Please do explain.

2nd Dec 2018, 5:01 PM
jamilxt
jamilxt - avatar
5 Answers
+ 7
Constructors are the code that is used to create objects. Constructors start with the short name of the class, followed by parentheses, that contain any parameters, and then an opening curly brace and a closing curly brace. In between the curly braces you assign values to instance variables and call any methods that need to be called when an object of this class type is created. Constructors may look like methods, but they neither return a value nor do they have the tag 'void'. You can have as many constructors as you want in a class, but each constructor needs a unique set of parameters. If you don’t write a constructor for your class, the compiler will implicitly include a constructor that contains no parameters.
2nd Dec 2018, 6:52 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 2
Before I start next time when posting please tag the language youre talking about, it makes it much easier and faster to help you :) From your profile I will assume you’re talking about Java. Default constructors are made by the compiler if no custom ones are provided in a class and are called just like user defined ones, default constructors are completely empty something like this: class MyClass { public MyClass() {} } So when you create an object you cannot pass any arguments
2nd Dec 2018, 6:22 PM
TurtleShell
TurtleShell - avatar
+ 1
Jamilur Rahman should be mostly the same
2nd Dec 2018, 6:26 PM
TurtleShell
TurtleShell - avatar
0
TurtleShell Thanks for correcting me. But, What about other languages? To be specific, I want to know in the way of Object Oriented Programming. That's why didn't include specefic language.
2nd Dec 2018, 6:25 PM
jamilxt
jamilxt - avatar
0
TurtleShell Okay, Thanks. Waiting for more persons to comment. 😊
2nd Dec 2018, 6:28 PM
jamilxt
jamilxt - avatar