+ 8
Is it possible to create constructor inside another constructor in java?If possible how?
I got the question "Constructor can be created into another constructor" I choosed false and it gave me the message wrong
26 ответов
+ 5
yes it's possible but we must use another class like this:
https://code.sololearn.com/c26sD2f58uVJ/?ref=app
+ 20
DecodesWorm Olamilekan
Friend,
I am sorry that my answers are too extensive ...
I want we go through and repeat the theory together, I hope, we will come to an appropriate conclusion.👍
//Take a look at LukArToDo 'c code!😉
+ 17
"Constructors are one of the simplest, yet trickiest object creation concepts in Java."
• Constructor chaining in Java — https://dzone.com/articles/constructor-chaining-in-java
Calling a constructor from another constructor, is known as constructor chaining. If we want to access the properties of any constructor then we have to use constructor chaining.
JavaBobbo
➝ this() (call to this) is used to call the same class constructor.
That mean we can achieve the properties of constructor which are present in the same class. For using this() constructor overloading is necessary. Without constructor overloading we can’t get the properties of same class.
• https://www.sololearn.com/post/61343/?ref=app
+ 16
DecodesWorm Olamilekan
Thanks a lot for nice words and support!😊
I will try to explain as I'm learned,
• Using this() keyword to call the current
class constructor within "same class".
• Using super() keyword to call the Super
class constructor from the "base class".
[ Edited: ]
https://code.sololearn.com/cMP01938G5ft/?ref=app
+ 16
DecodesWorm Olamilekan
Sorry friend, maybe I don't understand your question well!!?
We are explained everything above,
You can invoke other constructors from a constructor.
Explicit invocation of a constructor can be done only from within another constructor using this() method.
I think, calling the constructor more than once on the same object may not be possible, ... I can't say!
+ 15
What is need of constructor chaining ?
#1
Constructor chaining is used to access the properties of constructor in the same class or in another class.
#2
Using constructor chaining we achieve code optimization, means lines of code get reduced.
#3
Using constructor chaining maintenance easy because if we keep the common functionality in super class constructor. So we need to change only in super class constructor.
#4
If same code will repeat at multiple times then more memory will used. To avoid this we use constructor chaining, we access the same code. Then less memory used.
+ 15
• Java requires a constructor call for every object that’s created, so this is the ideal point to initialize an object’s instance variables.
Important points to remember:
• When you create an object, its constructor is called before any methods.
• When you dont't define a constructor, Java provides its own default constructor.
• If you define your own constructor then you have to decide what values should be given to the instance variables.
• Constructor can have any access modifiers like private, public, protected or default.
• this() and super() must be first line in the constructor code.
• Constructor overloading is possible but overridding is not posiible.
• It cannot be inherited.
➝ Constructor (object-oriented programming)
https://en.wikipedia.org/wiki/Constructor_(object-oriented_programming)
+ 15
DecodesWorm Olamilekan
• SUPER() Constructor Call
If you need to call superclass constructor from the subclass constructor then you will have to use the super() keyword in the subclass constructor. The syntax to call a superclass constructor is:
super(),
OR
super(Parameter_list).
The statement super() calls the no-argument constructor of its superclass and the super(argument) invokes the superclass constructor where argument must match.
➝ A constructor is used to create the object of the class. Unlike properties and method, the constructor of the superclass is not inherited in the subclass. They can only be called from the subclass constructor using the super keyword.
+ 15
DecodesWorm Olamilekan 😄🍻
This is my way of learning, and I'm really glad if it suits you!👍😀
+ 14
DecodesWorm Olamilekan
Thank you!😊
For the last question we need to call someone with much more knowledge than I have!🍻
The question is really good,👍 and I need to thinking about, becouse I also learning and I had not such experience.
+ 11
Yes, you can call a constructor from another constructor. 'Constructor chaining' is the technical term. Just try to search for that on the internet.
+ 7
I thought Genesis was asking about inner classes and not constructor chaining. But inner classes and their constructors don't usually appear within the constructor of the outer class.
+ 6
The question is about creating constructor inside another constructor not about constructor chaining
+ 4
Danijel Ivanović you actually invoked my memories of Java syntaxes and semantics rules thank you!!
+ 3
you can #call (not create) a constructor inside another constructor
+ 3
Danijel Ivanović love your explanation, but my question is do we use this() to invoke or to initialize the constructor of the super class or super() or we can use both
+ 3
Danijel Ivanović have u encountered a scenario where you have to call a constructor of a class inisde it's class
+ 2
But it was the word created
+ 2
My bad, i read it wrong i thought he mean the same class constructor.
+ 2
Danijel Ivanović bro seriously am enjoying your lesson I Used to be Java coder but right now am a web developer, so my another question is can this() be to involve instance variable of a parent class from its subclass, after subclass extends the parent class instead of using super()