+ 1

I have read from many books, still cant correctly seem to understand the constructor concept and its use

Can someone explain constructors concept

4th Apr 2019, 12:56 PM
Gayatri Palkar
Gayatri Palkar - avatar
8 Respuestas
+ 9
Constructors helps us in the construction on objects. It enables us to easily construct many objects with one skeleton. In simple words, let's suppose u want to create many girls object. So, u can create a sample blue print with all the properties that the girls will have like name, colour, pet e.t.c. Now, u can easily create girls objects with that blue print as u only need to write their properties in the sequence of blue print. Everything else will be done by the blue print. That blue print is actually constructor. You can also look at this code no matter which language u know. But this will help u in clear understanding. https://code.sololearn.com/c2rTtBY7TWU9/?ref=app
4th Apr 2019, 2:50 PM
Arushi Singhania
Arushi Singhania - avatar
+ 6
Don't worry, Palkar, you are at the right platform to learn about constructor, because the Java constructor lesson in Sololearn is awesome in having Try It Yourself code snippets for you to experiment easily. . https://www.sololearn.com/learn/Java/2157/ . When a class has no constructor or only one constructor, it is quite straightforward. So I think what confuses you is in the third tab of the above lesson, when you define more than one constructor in the same class. . The reason so is, something like default parameter of a function. So when creating new instance of class, if you put different arguments in the parenthesis, you can initialize the object in a different way.
4th Apr 2019, 4:27 PM
Gordon
Gordon - avatar
+ 5
Imagine a class and you want to create an object from it. Class myClass{ //some code } myClass my = new myClass(); You need the constructor to initialize your object. * if you don't write an own constructor: --> default constuctor: public myClass(){ } You will not see it but if I'm right the compiler add it. * own constructor: --> maybe you want to add values private int number; public Myclass(int i){ number = i; } --> maybe you have some variables you want to set when creating an object private int num; private int[] arr; public myClass(){ num = 7; arr[] = new int[num]; }
4th Apr 2019, 1:31 PM
Denise Roßberg
Denise Roßberg - avatar
+ 4
multiple constructors = constructor overloading. It works same as method overloading. So maybe this lesson could be also helpful: https://www.sololearn.com/learn/Java/2165/?ref=app
4th Apr 2019, 5:06 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
Can you explain what part of those explanations you read, that you fail to understand?
4th Apr 2019, 1:19 PM
Hatsy Rei
Hatsy Rei - avatar
+ 1
Balguruswamy book..it explains other concepts really well..but i just couldn't get the use of constructors
4th Apr 2019, 3:37 PM
Gayatri Palkar
Gayatri Palkar - avatar
+ 1
The comment in the original example code snippet in Sololearn lesson is not very clear, so I added some lines of comments for your easier understanding. https://code.sololearn.com/ciXHTtmeosTJ/?ref=app If this is not where you get stuck at, please input the code snippet in code playground and post the link to your code here so we can further explain based on specifically where you get stuck at.
4th Apr 2019, 4:37 PM
Gordon
Gordon - avatar