Kotlin Constructor - Primary, Secondary Constructor and Init Block

  Рет қаралды 19,565

Simplified Coding

Simplified Coding

Күн бұрын

Пікірлер: 35
@rxtechandtrading
@rxtechandtrading 2 жыл бұрын
you sir are one of the BEST youtube tutors i have ever seen, you should have tens of thousands of subscribers- best description of primary and secondary constructors- seriously!! keep up the good work
@SB-ox1co
@SB-ox1co Жыл бұрын
Thank you very much for this video. Coming from Java I was confussed about constructors in Kotlin.
@Matematica_Aplicada
@Matematica_Aplicada 2 жыл бұрын
Thank you very much! Nice explanation!
@Ari-118
@Ari-118 3 жыл бұрын
Thanks loved it!
@keshav661
@keshav661 2 жыл бұрын
HI Belal. Thanks for sharing this knowledge. My doubt is, as you mentioned, we cannot have any logic written inside the primary constructor. If we need to write then it should be within init block but there are few things like calculating the volume which is done inside the body of the primary constructor directly rather than writing in init block. How is it possible??
@soma7891
@soma7891 2 жыл бұрын
Probably are you referring to the volume property, that's is possible because you can write code inside the getter or the setter, but that code is called only when the getter or the setter are called. With the initializer blocks you have the code - generally is used for multiple lines of code - executed when the objects are created using the class.
@zakariarada1856
@zakariarada1856 4 жыл бұрын
Great video thanks bro keep on
@kishorramani007
@kishorramani007 4 жыл бұрын
Hi While creating instance of class, Secondary constructor's parameters value is assign first then init block is executed then secondary constructor's body execute? Why like this? If the parameters value is assigned first then body's println should print but it's print after init block
@SimplifiedCoding
@SimplifiedCoding 4 жыл бұрын
init block is called after primary constructor. And if you are making a call to secondary constructor then, in this case secondary constructor will call primary constructor and then init block. But when you call secondary constructor, primary constructor is called in the first line only and hence init block is also called and then the secondary constructor body is executed. Hope you got it.
@kakyoin4535
@kakyoin4535 11 ай бұрын
Thank you so much. After i finish this oop i will build my android!
@olahuuber5540
@olahuuber5540 4 жыл бұрын
what is the use of multiple init block
@SimplifiedCoding
@SimplifiedCoding 4 жыл бұрын
Haha good question, I have not came across any practical scenario where I had to create multiple init blocks. And it is recommended to have only one init block of required. So I can say there is no use of it.
@kubux6426
@kubux6426 4 жыл бұрын
Could you make video about kotlin multiplatform?
@SimplifiedCoding
@SimplifiedCoding 4 жыл бұрын
Yeah I have plans about it
@muskanyousaf20
@muskanyousaf20 4 жыл бұрын
1st 🥰Will Surelly watch These kotlin Toutorial 🙂 Thanks for the Toutorial Please Make a Video on Firebase Pushed Notifications #request
@anil801
@anil801 4 жыл бұрын
what is use/purpose of "lateinit" in Kotlin?
@SimplifiedCoding
@SimplifiedCoding 4 жыл бұрын
Purpose of what?
@anil801
@anil801 4 жыл бұрын
@@SimplifiedCoding sorry, type error..its what is use/purpose of "lateinit" in Kotlin?
@SimplifiedCoding
@SimplifiedCoding 4 жыл бұрын
For this scenario #1 you cannot assign a value to the variable while declaring it #2 as you cannot assign a value, you can use null as in kotlin you have to give a value while declaring a variable #3 If you give null its ok, but the problem is from now onwards everywhere you need to use null safety operator with that variable #4 you can use lateinit when you dont want to use null safety But it may case a null pointer exception because lateinit means you dont have the value now but you will give it later. And you do not need to use null safety with lateinit. But use it only when you are absolutely sure that it will not be null.
@bhuvneshvarma7843
@bhuvneshvarma7843 4 жыл бұрын
Thanks for the video!Your one observation is wrong in secondary constructor example. You said "First init block is called,then secondary constructor is called and then secondary constructor will call primary constructor".Wrong.The init block is always called after the primary constructor. So first primary constructor is executed.Then immediately init block starts to execute.After init block , secondary constructor block starts to execute.
@SimplifiedCoding
@SimplifiedCoding 4 жыл бұрын
I think I said exactly what you are saying. Anyways I will watch the video. May be I said wrongly. 👍
@yotsusan_machi
@yotsusan_machi 3 жыл бұрын
oooh so the constructor with body is like init block wow thanks for this amazing knowledge btw how to use secondary constructor for app development in android phone
@footballCartoon91
@footballCartoon91 2 жыл бұрын
despite of the many implicit functionality of creating objects under the hood sometimes we are confused whether it is implicit or explicit
@vengateshm2122
@vengateshm2122 4 жыл бұрын
So the order is Primary constructor Init block Init block Secondary constructor
@SimplifiedCoding
@SimplifiedCoding 4 жыл бұрын
No init block is called after primary constructor. And if you are making a call to secondary constructor then, in this case secondary constructor will call primary constructor and then init block. In any case primary constructor has to be called. And if you have multiple constructor then only the calling constructor will be called and then primary constructor and then init block. But when you call secondary constructor, primary constructor is called in the first line only and hence init block is also called and then the secondary constructor body is executed. Hope you got it.
@vengateshm2122
@vengateshm2122 4 жыл бұрын
@@SimplifiedCoding yeah.thank you.
@jishnupdileep3143
@jishnupdileep3143 2 жыл бұрын
👏🏻👏🏻👏🏻
@bilelrahmouni01
@bilelrahmouni01 Жыл бұрын
thank you
@beypazariofficial
@beypazariofficial Жыл бұрын
why indian people always have to use "sir" phrase in their comments? and why are they so formal? its just youtube
@denkling
@denkling 3 жыл бұрын
You should consider to become a teacher!
@ipvoodoo
@ipvoodoo 4 жыл бұрын
I didn't understand anything at all
@SimplifiedCoding
@SimplifiedCoding 4 жыл бұрын
Can you please be a little specific about what exactly you did not understand?
@richenjoshi7054
@richenjoshi7054 2 жыл бұрын
true
@rxtechandtrading
@rxtechandtrading 2 жыл бұрын
mabey u dont understand because you have put no effort into learning this language..This man has perfectly described the concepts of primary and secondary constructors in Kotlin
Kotlin Inheritance Tutorial - Inheritance with Example
9:40
Simplified Coding
Рет қаралды 11 М.
Kotlin Classes and Constructors - Primary vs Secondary
13:51
Will Tollefson
Рет қаралды 604
Sigma Kid Mistake #funny #sigma
00:17
CRAZY GREAPA
Рет қаралды 30 МЛН
We Attempted The Impossible 😱
00:54
Topper Guild
Рет қаралды 56 МЛН
Kotlin Generics Explained with Example
10:18
Simplified Coding
Рет қаралды 18 М.
Kotlin Standard Delegates - Lazy, Observable and Vetoable
10:42
Simplified Coding
Рет қаралды 10 М.
Kotlin Interface Tutorial with Example - Kotlin Multiple Inheritance
12:54
How To Think Like A Programmer
1:00:07
Coding Tech
Рет қаралды 2,1 МЛН
I Helped 2,000 People Walk Again
15:31
MrBeast
Рет қаралды 12 МЛН
Kotlin Class and Object
11:19
Simplified Coding
Рет қаралды 20 М.
How To Speak Fluently In English About Almost Anything
1:49:55
EnglishAnyone
Рет қаралды 3,1 МЛН
Constructors and Init blocks in Kotlin - BEST PRACTICES
5:33
Rahul Pandey
Рет қаралды 19 М.
7 Outside The Box Puzzles
12:16
MindYourDecisions
Рет қаралды 163 М.