Podcast Lookup

Java Constructors Explained: Constructor Overloading, Chaining & the this Keyword | Full Java Course

The Java Developers Hub

  • December 3, 2026
  • 58 min
  • Episode 40
Episode page
About this episodeLearn how constructors work in Java and how they are used to initialise objects effectively. In this episode, we explore Java constructors…
Learn how constructors work in Java and how they are used to initialise objects effectively. In this episode, we explore Java constructors from the fundamentals to more advanced concepts, including constructor overloading, constructor chaining, and the use of the this keyword.Understand how constructors differ from regular methods, how Java selects the appropriate constructor when creating objects, and how to reuse constructor logic to write cleaner, more maintainable code.Whether you're new to Java or strengthening your object-oriented programming skills, this tutorial provides practical explanations to help you understand object initialisation and constructor behaviour.What You'll Learn
  • What constructors are and why they are important in Java.

  • The difference between constructors and regular methods.

  • Default and parameterised constructors.

  • How constructor overloading works.

  • How to implement constructor chaining.

  • How to use the this keyword to refer to the current object.

  • How to call one constructor from another using this().

  • Rules and best practices for writing Java constructors.

  • How constructors support object-oriented programming.

Who Should Watch?
  • Beginners learning Java programming.

  • Students studying object-oriented programming.

  • Developers preparing for Java technical interviews.

  • Software engineers looking to strengthen their understanding of Java object initialisation.

  • Anyone following a complete Java programming course.

Transcript

From the publisher

This transcript comes from the podcast’s own feed, shown as the publisher wrote it.

Java Constructors Explained: Constructor Overloading, Chaining & the this Keyword | Full Java Course

00:00—
  1. Speaker 1

    Yes, so let's start. Welcome back Coder Army to our Java series. And today we are going to continue our object-oriented programming. And today we will see what are constructors, what is this keyword, and many more things. So it will be fun. So let's go to the screen and start. Yes, so what I have done here is that I have taken a project called demo.java And we have understood the meaning of boilerplate code Now we will make a student class So what we have done is that we have made a student class And let's say what all things we keep in student We keep a name of the student We keep his age We keep his roll number And we keep the name of the college in which he is in College So we have named, age, role number and the quality Then we know that if we had to make an object of this class Then what we used to do? We used to write student And the reference variable name was S1 And then we used to write new keyword and student What used to happen with this?

  2. Speaker 2

    It used to become an object of this Right?

  3. Speaker 1

    Very good Now if we had to add values to this object Then we used to add like this S1. name became Let's say Aditya S1. age became Let's say 28. We used to do something like this. Okay, now what happens if I don't do all this. I forget to do all this. And directly system.out.println. And I try to print the name of S1. I try to print the name of S1. And what do I do in the same way? I try to.

  4. Speaker 2

    Print the age. In the same way, I will print the roll number and college. So age. Here comes our roll number. And here comes our college. Okay. What do you think?

  5. Speaker 1

    Till now I haven't put any value in S1' s reference variable. I haven't put anything like S1.

  6. Speaker 2

    Name, S1. age.

  7. Speaker 1

    I made a straight class and tried to print it. So what will happen? Will there be any error? Let's run it once and see. No, we didn't get an error, we got some output. What is the output?

  8. Speaker 2

    Instead of s1. name, it got printed as null.

  9. Speaker 1

    Instead of age and roll number, it got printed as 00 and instead of college, it got printed as null. Now, what does this mean? Look, we call this default values. What are the default values? Let's discuss this a little bit. First of all, let's go back to our class. I had said that all the variables that we declare in our class, what does it represent? It represents the information of a real-life object or its data or its characteristics.

  10. Speaker 2

    We can say anything.

  11. Speaker 1

    If we make some functions here, for example, void mark attendance.

  12. Speaker 2

    I made a function void mark attendance.

  13. Speaker 1

    Which will mark the attendance of a student and let's say nothing happens here we will do simple system.out.println attendance marked for student and whatever is the name of the student let's say name.

  14. Speaker 2

    Something like this, right? This would happen.

  15. Speaker 1

    Now, the functions that we write in a class, what we call it, in a real life object, it represents someone's behaviour. We had studied all this in the previous lecture. Now, let me say one thing. In any class, the variables that represent the information and characteristics, we know them by another name, which we call instance variables. And the methods, the behaviours that represent the functions, We call them Instance Methods. So basically, whatever function we write in a class, we can call it Instance Methods or just Methods. We read this because remember, Wait a minute, let me comment all of them for once. Let's assume that we haven't created any object named student, our main is still empty. And till now, as we used to write code, we used to declare some variables here, like int x equal to 4. Let's assume that I declared this variable. Now what do you think, which variable is this x? In this case, we call it a local variable. Here we called it an instance variable, here we are calling it a local variable.

  16. Speaker 2

    Okay, why local? We had talked about this, that the scope of this.

  17. Speaker 1

    Variable x, till when it is the scope of this main function, right? As soon as this main function ends, the scope of our x ends, right? Now, one more thing, this x of ours, what is this? This is a primitive data type, because it is an integer, okay? So, this x of ours, which memory will it be stored in? It will be stored in our stack memory, and when will its scope end? As soon as our this The scope of the main will be finished. So, we said that x is our local variable. Now, what happens is that whenever you make instance variables, that is, whenever you make a class which has variables, when you make an object of our student class using new keyword, then you know where our object will be made. In heap memory, that is, all our instance variables in this object, Where will all these be?

  18. Speaker 2

    In heap memory. Right?

  19. Speaker 1

    So, because all these are in heap memory and are part of an object, we call them instance variables. And the second thing, they have default values. What are default values?

  20. Speaker 2

    Okay.

  21. Speaker 1

    Default values are basically if we have I didn't assign any values to them. For example, I tried to print s1 name, age, roll number and college. I didn't assign any values. So what values do they get by default? Those are the default values. Now, for every data type, the default value is different. So if your data type is integer, let's write it here. So if your data type is integer then your default value will always be 0 Similarly if your data type is floating point numbers then your default value will be 0.0 And similarly if your data type is a boolean then your default value will be false And in the case of our strings we have given them null Now many people must have heard for the first time what.

  22. Speaker 2

    Is null because we haven't introduced null yet Null kuch nahi hota.

  23. Speaker 1

    Whenever you give a default value to an object, you give it null.

  24. Speaker 2

    Null means nothing. If you understand it in simple English, then null means nothing. No value. Nothing.

  25. Speaker 1

    Now, we can't call it zero because it's a string. You can't give a zero value to a string. We could have given it an empty string. But in Java, we prefer to give it null values. So, in Java, whenever you declare an object, and the reference variable of that object, we give its values null. This means that our string is not a normal data type, it is a non-primitive data type. And if it is non-primitive, it means that it is a part of classes and objects. String is a class and we make objects out of it. So, if we write string college, then college will be a reference variable. Now, because this college is a reference variable of this string, and we didn't put anything in this reference variable. We didn't write s1. college equal to some value, right? So, this means that by default, what is stored in it? Null is stored.

  26. Speaker 2

    Okay?

  27. Speaker 1

    So, you have to remember, all our non-primitive data types, which are reference variables, in that, our default value is null. And if you talk about primitives, then in the case of integer, it is 0, floating 0.0, boolean, false, and so on.

  28. Speaker 2

    Okay? Okay. Okay. This is understood.

  29. Speaker 1

    After this comes an important question. What if I declare this local variable. And then try to print it. System.out.print ln and I try to print x.

  30. Speaker 2

    So what will happen?

  31. Speaker 1

    See, I got a compiler error which says that the local variable x may not have been initialized. This means that the default values of the local variable are not there. No default values. But why is it that local variables don't have default values? But our instance variables have default values. We only know this difference that instance variables are declared in heaps and local variables are in stack memory.

  32. Speaker 2

    So why this difference? Look, it's simple.

  33. Speaker 1

    If Java does anything, it will do it because it can optimize its language. Now, because the variable you declare in heap, there is a very high chance that it will stay there for a long time. Because if you make an object, there is a very high chance that you use that object.

  34. Speaker 2

    Again and again.

  35. Speaker 1

    But these local variables like x, its scope will end only when the scope of our function ends. But you will say that this is a main function. Everything is written in main. So the scope of main will last for a long time. But main is also a function. It didn't make a rule for main. But let's say you make a different function here. Let's say void. Fun, I gave a function named fun. And here I declare a variable int x. So its scope ends only when the scope of fun ends. So Java believes that because it is a local variable, a variable that exists in a function, and its scope is very limited, so I don't need to give default values to it. To keep my Java language further optimized, I don't give a default value to it. And I make it mandatory that if you want to use this variable, then first give some value to it.

  36. Speaker 2

    Right? Look, then we get this error.

  37. Speaker 1

    The local variable x may not have been initialized.

  38. Speaker 2

    Okay.

  39. Speaker 1

    So, I hope you understood that the instance variables have default values and our local variables don't have default values. Okay.

  40. Speaker 2

    Very good. So, what I do is, I comment all of them for now. Sorry. Yes, I comment them. We don't need them. But we need them. Okay. And we need them too. Okay.

  41. Speaker 1

    So, I told you what are the default values. But now, what are the default values? I want to give values in this. So how would I do it? I would give something like this.

  42. Speaker 2

    S1. name equal to aditya. S1 dot age equal to 28.

  43. Speaker 1

    Second value is S1 dot cultural number equal to 101.

  44. Speaker 2

    Let's say. And S1 dot college. College was left. So, let's give this too. IIT Guwahati. Okay. Very good.

  45. Speaker 1

    So I have given their values. Now if I print them, then I should get the right values. Which is happening. I have seen this before. Now my question is that whenever I declare an object and I don't want its default values, I really want to give some values, then I have to follow this procedure. In S1, all the variables have to give a value. But you think this is a very tedious process, right? I am assigning a value to every variable one by one. What if I forget about a variable? Now, I made this myself. So, I knew that name, age, role number, college are four things. But let's say someone else made it and gave it to me. And I would have forgotten that name, age, role number, college, sorry. Apart from college, let's say one more thing is grades. I would have forgotten to assign it. If I had forgotten, it would have gone to default value. But I didn't want that. So, as I declared a student again and again, I had to give all its values one by one.

  46. Speaker 2

    Right?

  47. Speaker 1

    What happens is, when this object is being created, when this object is being created using this line, I can add some values to it. This topic is called Constructors, which is also the name of our topic today. Constructors means nothing, it means to create an object. You construct an object and when you are constructing it, you add some values to it.

  48. Speaker 2

    Okay, how do we add that? Let's see that.

  49. Speaker 1

    So first of all, let's go to the iPad and then we will understand it here in the code. Yes, so we are going to talk about Constructors in Java. Okay, so first of all, what I do is that I put my class student, Usi ko dubara se bana leta hu. Usme mere paas kya kya tha? Basic basic cheez rakh lete hain. Humare paas tha name. Humare paas thi age. Humare paas tha roll number. Aur humare paas tha college. And then this class used to end. I have left some space. Now we will fill things in it. I will tell you what. So this was our class. Now I told you that whenever I used to make the object of this class. Whenever I used to make the object of this class. I had to write this line. Student s1 equal to new class.

  50. Speaker 2

    Student. Very good.

  51. Speaker 1

    Now, if I have to give some values to these variables, then what I had to.

  52. Speaker 2

    Do was, s1. name, s1.

  53. Speaker 1

    Age, something like this. I had to write syntax. And I said, our constructors are useful here. What they do is, they give some values to an object at the time of creation.

  54. Speaker 2

    Now, how would they do it?

  55. Speaker 1

    First of all, understand its syntax. First of all, where will we make the constructor? We will make it here. Constructor will look like a function, like a method. But it has some rules. What are the rules of constructor? Let's discuss that first so that it is easy for you to make a constructor.

  56. Speaker 2

    Rules of constructor. These are its basic rules.

  57. Speaker 1

    Which I am writing here. First of all, the name of constructor, whatever we will name the constructor, It will be the same as the name of our class. So, the same name as class. You have to remember this. I will tell you why all this. Secondly, the constructor, as I said it is a method. So, it is a method but it is a special type of method. It is a special type of function. So, it doesn't have any return type. No return type. Now there is no return type. It means it must be void.

  58. Speaker 2

    No.

  59. Speaker 1

    Not even void. Its return type is not even void. We just leave it empty. Again I will tell you why. Third, this constructor is automatically called. As soon as we declare an object. So automatically called During object creation. When we create that object.

  60. Speaker 2

    Fourth.

  61. Speaker 1

    Now, what is this basically used for? This is used to initialize a new object. Used to initialize.

  62. Speaker 2

    An object. Right?

  63. Speaker 1

    This is what we were doing with it. And there is a fifth rule for this. Now, because it is like a method. Constructor is like a method. So, it can also be.

  64. Speaker 2

    Overloaded.

  65. Speaker 1

    We have talked about method overload and function overload. So it can also be overloaded. We will see about this also. So these are some rules of a constructor. We have to follow all these rules. I will also give you the reason of the rules. Why we follow this internally. So first of all let's go towards our class and make our constructor. Okay, so what I will do is, I will make a constructor here. So first of all, I said that our constructor is like a method, but its name will be the same as the name of our class. Our class name is student, so I will make constructor student. Okay, now because I said it is like a method, so obviously parameters will come in it and curly bracket will come in it, something like this. Now here we write return type of any method But it doesn't have any return type, it doesn't have void either So I'll leave it empty So its syntax is something like this Student bracket and my curly bracket start, curly bracket end Something will come in between Do something, I'll tell you what So we followed some rules here Same name as class, no return type, not even void Now let's move ahead Now what will come in this This is very interesting. In this, I can provide a value to all my variables. Those values which they will get automatically whenever a new object is created. For example, I can write whatever my name is, by default, add it as Aditya. Because if I do nothing, then it will get null value. I don't want it to get null. I want it to get Aditya instead of name. Similarly, I want it to get 28 instead of age.

  66. Speaker 2

    Roll number... and let's say college becomes IIT Guwahati. I want something like this.

  67. Speaker 1

    Now, as soon as I do this, as soon as I make this, now when I make an object of this class, student s1 equal to new student, and I do system.out.println s1. name, s1. age, s1. rolumber, then I will get all these values.

  68. Speaker 2

    Right? What will I get? Aditya 28, 101, IIT Guwahati.

  69. Speaker 1

    This is the whole concept of our constructor. We will see this in code. But the most interesting thing we will have to understand. That we said. Here we have a rule also written.

  70. Speaker 2

    Where is it written? Yes.

  71. Speaker 1

    Automatically called during object creation. That when you are creating an object, using this line, student s1 equal to new student. So I said, this constructor, it calls automatically. How do you call a method? You write its name, like student. And then you call it in this way. But we didn't call it like that.

  72. Speaker 2

    We did it. We did this. Look at this.

  73. Speaker 1

    We wrote here, student And our parenthesis and semicolon. In the same way, we call a method. So this line, automatically called during object creation. Actually, it's not automatically called. We are calling it. We just don't know. Because it's a part of our syntax. We always, in syntax, if I write it in a different color. We always write in syntax, new student. and here curly bracket. So now you must be understanding that I understood the new thing in the last lecture that dynamically allocates memory in heap memory. But why do we write this in the future? So now you must have understood that we write this to call our constructor. So, in our class, whatever constructor is made, we are calling it using this keyword, student and whatever is the name of our class and our curly bracket.

  74. Speaker 2

    Right? So, I hope you have understood.

  75. Speaker 1

    The meaning of this whole line very well that S1 is our reference variable, student is our data type, our user defined data type and our class name is there.

  76. Speaker 2

    What does new do? It allocates memory dynamically. So, what would be happening?

  77. Speaker 1

    As you have written new, first of all, our heap memory, in that, an object's memory is allocated. And when you call the constructor, the name, age and roll number will get the default value.

  78. Speaker 2

    So let's say here comes Aditya instead of name.

  79. Speaker 1

    Here comes 28 instead of roll number. Here comes 101. Sorry, instead of H it's 28, instead of roll number it's 101, here IIT-Coharty is here So they got default values, after that when this line gets executed Then we are assigning this to S1, and S1 is inside the stack memory, this is our stack So here will be S1, who will get this reference of this whole object Reference means, we will get the address of its starting So if it's 1001 address, then S1 will store 1001, and this was our heap memory Now you must have understood the meaning of this line when we write s1 is equal to new student. But now you must have a question. We have made a constructor here. That's why we wrote this line. But till now we were not creating this constructor. If I copy our class again. Till now we were not writing this code in our class. We used to write only this part. We didn't make any constructor here. But even then, when we used to make an object of this class, then what was our syntax? We used to write student s1 equal to new. And here again, we used to get the same syntax, student. And then this parenthesis. So in this case which constructor are we calling? Here it is not a constructor. Here the concept comes of default constructor. Default constructor. What is default constructor? Look, Java says that constructor is mandatory. So Java says that constructor is mandatory. So either you make it yourself, which we made up here. Look, we made a constructor by ourselves. But if you are not making a constructor by yourself, which we haven't made yet, then Java inbuilt will make a constructor by itself, which it will call default constructor.

  80. Speaker 2

    What will it call? Default constructor. It will make a default constructor.

  81. Speaker 1

    Now how will the default constructor look like? As soon as this code gets compiled, Java will add a default constructor in this class. So it will look something like this. What will be the name of this constructor? First of all, the name of our class. Then parenthesis. Then we will open its brackets. Now look, what values does it give? Now Java doesn't know that name, age, role number and college. If you want to store Aditya in name and age, let's say you want to store 28, then Java doesn't know. Give this to yourself, which we tried to give here.

  82. Speaker 2

    If we give.

  83. Speaker 1

    Values to ourselves, then Java will say, okay, I understood what the values are. Now if you print s1. name, then I will print Aditya. If you print s1. age, then I will print 28. Now Java knows. But here, because we directly wrote student-discipline-news-training, and we didn't make any constructor, so Java made this constructor of its own inbuilt. Now, what will Java write in this? It won't write anything. This will remain empty. This is empty here. This is our complete default constructor. But now what will happen when I try to print s1. name or s1.

  84. Speaker 2

    Age?

  85. Speaker 1

    Nothing will happen. We just talked about this above. Which values will be printed? Default values. The default values will be printed from the instance variable. And we know the default values of the instance variable. If there is a string, it will be null. If there is an integer, it will be zero. If there is a floating point, it will be 0.0. If there is a boolean, it will be false, etc. So we know the default values. So by default, the default values will be printed. So this constructor makes Java by itself. When you don't make it yourself.

  86. Speaker 2

    Right? Okay, very good.

  87. Speaker 1

    Now let's move one step ahead in our constructor. Let's copy the same class and remove the constructor. So I said that we can make a constructor and we can add our own default values in it. Name, age, roll number and college. But what we were doing till now, we used to write constructor here, student.

  88. Speaker 2

    And here we used to add default values in it. Like add Aditya in name.

  89. Speaker 1

    But I am doing this hard code. Means, as soon as I made the class, that's when I told you what the default value is. You know, put Aditya in name, put 28 in age, etc. But what if I want to dynamically decide what its value will be. So basically, when I am creating this object, when I am writing such a line, student S1 equal to new Student. Here I can somehow tell that what I have to keep these values.

  90. Speaker 2

    And not by doing one by one. S1. age, S1. name.

  91. Speaker 1

    Not by doing like this. Because what was the problem in this? One was a very tedious process. And second, it could be that I forget something. So, let it be mandatory to me. That no, till I don't let you create the object. Till you don't let me put these values. So, here we have a new type of constructor. Which we call parameterized constructor. Parameterized constructor.

  92. Speaker 2

    This constructor, okay?

  93. Speaker 1

    This one we have read above, this was your default constructor, which Java makes by itself.

  94. Speaker 2

    I have written it here.

  95. Speaker 1

    And now what we are reading here, this is our parameterized constructor.

  96. Speaker 2

    Okay? Okay, very good. So let's look at the parameterized constructor. So what did I say?

  97. Speaker 1

    I have made a class of my own, student. I don't have any constructor here. I want that when I am declaring an object, when I am making an object, then only I can add all these values. And it is mandatory for me to add all the values. Until I assign all the values, I can't go ahead. So that I don't forget to add someone's value. So there comes the parameterized constructor.

  98. Speaker 2

    Nothing happens. You again, I will make this again. Yes.

  99. Speaker 1

    You again make your normal constructor, student, which will be the constructor's name. You open the bracket. Now, because it's a function, you can also take parameters here.

  100. Speaker 2

    Right?

  101. Speaker 1

    Just like we take in normal functions. So, what I do is, I expect some parameters here. That you give me some string, some name. Now, I'm keeping the name of its variable separately.

  102. Speaker 2

    Okay? Because here it's already named, so I'm doing string n here. Simple. Okay?

  103. Speaker 1

    Here, apart from this, you give me some int age. I keep it as a. You give me some int... Let's say roll number rn2 and similarly give me int collegec When you give me all these values, what will I do? I will put n in our name, age in a and our roll number In that I will put my Rn This roll number is RnO So I will put Rn in it And in college I will put C in it Something like this.

  104. Speaker 2

    So what did it do?

  105. Speaker 1

    Name means this name Which value will I put in it? N means this one which is coming Same way it defined the whole constructor Now when you will make its object So what will happen is that you will not make the object like this. Because now you will have to pass the parameters. So I will make it again and show you how we will make the object. Now we will make the object like this. We will write student s1 equal to new. Here I will write student. Now we will open the bracket and pass the attributes of this parameter. Basically, as we used to call the normal method by giving the parameters, we are calling the constructor by giving our attributes.

  106. Speaker 2

    So, I will pass the value from here.

  107. Speaker 1

    Let's say 28H ki jaga 101 roll number ki jaga aur IIT Guwahati college ki jaga mujhe yeh kuch values deni hai aur mine se call kar diya toh ab mera syntax ho gaya yeh thik hai maine kya kiya maine apne constructor ko apne parameterized constructor ko uski kuch values de kar ke call kar diya thik hai bohot badiya I have already done this. Okay, what if I don't want to give all these values. In this function, I want the user to be able to give all these values.

  108. Speaker 2

    Like here, name, age, number, college.

  109. Speaker 1

    Or normally, like till now we were creating an empty object. We can make that too. So, if I write like this, again, let's write it in white. In the next line, I wrote student. Now, I will make another object, s2. Equal to new student. And I will leave it empty. then you will get compiler error here. And that compiler error will come because it will tell you that you have to give parameters here. Because the constructor we have now expects all these values.

  110. Speaker 2

    So you can't just call here.

  111. Speaker 1

    But we just said that there is a default constructor that Java makes by itself. That is always empty. But the rule here is that the default constructor is made only when you don't make your own constructor. Once you have made your own constructor, whether you have made it default or parameterized. java doesn't make a default constructor from inside so now there is only one constructor which is this parameterized constructor and it expects all the values but you don't want to give so in that case what will you do? in that case we will overload the constructor ok? what does overload mean? we have already read overload what.

  112. Speaker 2

    Happens? you think what.

  113. Speaker 1

    Happens that I make another constructor here let's say student and leave it empty take this.

  114. Speaker 2

    I have left it empty.

  115. Speaker 1

    Now, what has happened to us? Default constructor has also been created. A parameterized constructor has also been created. Now, it doesn't take any argument. Sorry, it doesn't take any parameter. And it takes from the parameter. So, what has happened to us? Our method has become an example of overload. We have overloaded the constructors here. So, now this error will also be removed. I can also make an object like this.

  116. Speaker 2

    Right. I will also mark this once. This. So, I can make an object like this. I can make an object like this.

  117. Speaker 1

    Because I have all the things, I have default constructor also exists here And I have parameterized constructor also exists here Right? So till now what we have done is, let's go quickly And let's execute it And after this let's move to our next keyword this Okay? So see what I did here, I made a file by doing demo2 So that you don't do.

  118. Speaker 2

    All the things in it, right?

  119. Speaker 1

    Let's leave it like this And here let's make our student class again So I will copy it from here This is my student, sorry This is my student class something here, and I will remove all of them, all of them are written there. Let's leave this behavior, this method, mark attendance. What I had said, first of all, if I create an object here, student s1 equal to new, sorry, new student, see, so here we had called student in this way, here we would have called constructor only, which constructor would have been called, default constructor, because there is nothing here, right, but And this is also allowed here, obviously, right? Because Java must have made a constructor by itself. But if I want to make a default constructor of my own, then how can I make it? Something like this. Student or essay. And I can give values here. Let's say name I gave Aditya. Age I gave 28. Role number I gave.

  120. Speaker 2

    Sorry. Role number I gave. Let's say 101. And college I gave. I antiquated. Something like this. Right?

  121. Speaker 1

    Now if I try to print all of these things, I will copy it from here.

  122. Speaker 2

    Something like this. So now you know what should be printed. All the values in our default constructor should be printed. So let's see this too. Absolutely. Our values are printed. Okay.

  123. Speaker 1

    If I leave everything empty here. I don't write anything here. I leave everything empty. So I have commented it for now.

  124. Speaker 2

    Let's remove it.

  125. Speaker 1

    I would have left everything empty. So this would have been the default constructor that Java makes by itself. So I don't need to write this, it's optional. This is the default constructor. This is made by Java itself, so I don't need to write it. So what would have been printed? All my default values would have been printed.

  126. Speaker 2

    Null, zero, zero, null. Now let's make a parameterized constructor. So I do one thing.

  127. Speaker 1

    I I will create a constructor named student here because I can overload it. And I will give some values here. Let's say int n, I need a string. I have a name expected n, an age expected, a roll number expected and I have a college expected.

  128. Speaker 2

    And what do I do?

  129. Speaker 1

    I put n in name, which is my name variable. I put a in age, whatever i is. I put rn in role number, whatever it is. And I put c in college's name, whatever it is. Now I can make my variable like this. So let's say I put Rohit here. And we have given 28 for age, 102 for role number, and for college too, IIT Guwahati. Something like this. So now this is my parameterized constructor. Because I called the constructor by giving all the values. Now when I will try to print them, then see, I will get Rohit's details printed.

  130. Speaker 2

    Now what did I tell you?

  131. Speaker 1

    If I remove the default constructor from here for some time, then I can't make a variable like this. Student s2 equal to new. Student, I can't write simple like this. Look, I got compiler error. What is it saying? The constructor student is undefined. Because this constructor doesn't exist. Because we only have parameterized exists. Because default doesn't exist. Because Java didn't make it by itself. Because you gave it by yourself. So, we have to make this by itself. If we want to create object like this.

  132. Speaker 2

    Okay.

  133. Speaker 1

    Now, let's go back to our iPad. And let's see what is this keyword. And how good is its use here.

  134. Speaker 2

    So, look.

  135. Speaker 1

    What is this keyword? First of all, obviously, I have written this here. This is a keyword in Java.

  136. Speaker 2

    Okay.

  137. Speaker 1

    What does it do? It denotes the current object. What is being declared. What does it mean?

  138. Speaker 2

    Okay. Let's understand this directly from the example.

  139. Speaker 1

    So, look.

  140. Speaker 2

    Again, what did I do? I took my class here. My student class. As I had taken it till now. In that, our name, age, role number and college is written. Okay.

  141. Speaker 1

    So, now let's see what this keyword is. First of all, what does this keyword do?

  142. Speaker 2

    What did I say? It refers to our current object. So let's say if I make a constructor here Student And if I make it parameterized Then I will expect a string here Name, this time I will keep the name of the variable same which is there Expect an age Expect a row number I keep everyone's name same And expect a college String college Something like this So what will happen? You have to understand it like this I have to assign a name here So in this name I have to assign this name.

  143. Speaker 1

    So I can write something like this. name equal to name I can write something like this. But it's confusing, right? It's confusing for Java as well. Because when I'm writing a name here, which name am I talking about? This one or this one? In this, who am I talking about? This one or this one? It's confusing, right? So what can I write here? First of all, I use this keyword. And I write something like this. this.name Equal to name. Now whenever I write this. And then I put my dot operator. So java understands. That what I am talking about. I am talking about my instance variable. So I hope you understood this from example. This says. Whatever current object is. I will store its reference. Like we used to make object. Student s1. Equal to new.

  144. Speaker 2

    Student.

  145. Speaker 1

    S1 is also a reference variable which stores the student object's reference. This is also a reference, but which reference? Whatever current object is running, which we are trying to make, it stores that.

  146. Speaker 2

    Okay?

  147. Speaker 1

    So whenever we write this, what happens from inside? It stores an object of this student reference.

  148. Speaker 2

    And.

  149. Speaker 1

    Name means its name. Like we write s1. name, exactly the same thing. Now, similarly, I can write this. dot age equal to age. Now, Java doesn't have any confusion because we are talking about this dot age and we are talking about this simple age.

  150. Speaker 2

    And in the same way, I can write everything.

  151. Speaker 1

    I can write this dot, your roll number, this dot, college and so on.

  152. Speaker 2

    I can write everything as it is. 101 or IITG, let's say this.

  153. Speaker 1

    There is no problem, there is no confusion because I used this keyword. Now this keyword becomes optional if this variable and this variable are different. Because Java is not confused if I write n here, write a here, write rn here, write c here.

  154. Speaker 2

    So Java is a different variable, it is not confused.

  155. Speaker 1

    But because the names of the variables are the same here and here as well So we have to use this keyword But you will say that this is the only reason for using this keyword No, there are many other use cases for this keyword as well In fact, we will talk about that only slowly But just remember one thing We always prefer this name and this name to be the same The variable names should be the same Why do we prefer? Again, it's a Java convention that we always prefer to write this.name equal to name, this.age equal to age. Because it's very easy to read, it's very easy to understand that which four values we have assigned to these four variables.

  156. Speaker 2

    Simple. You can find out by looking at it. Okay?

  157. Speaker 1

    Now, what is the next use of this? When we overload the constructor, it does a very good thing which is called as chaining.

  158. Speaker 2

    Now, let's see what chaining is. It's very interesting. Come on. Look, we are going to talk about chaining. In constructor ki.

  159. Speaker 1

    Now before understanding chaining. What we have to do. We have to learn to overload a constructor. Constructor overload. Which we have already done. We will do it again. After that, we will see what the chaining can do to us by overloading. Once we overload the constructor, our long lengthy code will be written. And then we will use the chaining to reduce that code a little. Now you will understand everything. Now let's go straight to the code so that more things are clear. So again I have made a new file called demo3.

  160. Speaker 2

    Java.

  161. Speaker 1

    And I have made my student class in which there is no constructor. So what if I want to overload the constructor So first of all I write my default constructor student Something like this I can write it in the next line also like this But the smallest syntax is written like this In case I want to create an object In which name, age, role number and college have no default value They get the normal default null, int instead of 0 and again null.

  162. Speaker 2

    After this, I will create another constructor in which I will give a value of this name. So, I will expect the string name from the user and write this.name. Again, you know why I have written this because I want to refer this name. Equal to name.

  163. Speaker 1

    Now, I will tell you one more thing. If I had not written this, then what would have happened? We didn't get any error. So what does this yellow line mean? It's a warning. And see what this warning is saying. The assignment to variable name has no effect.

  164. Speaker 2

    Why? Why won't it have an effect?

  165. Speaker 1

    Because Java doesn't know which name we are talking about. So Java thinks that we are talking about this name twice. Name equal to name. Put whatever is in the name back in the name. So what is it doing here? It puts whatever is in the name back in the name. So there won't be any effect.

  166. Speaker 2

    So if we write this here, then the value of this name will never go in this name. If we had written n here and we had written n here, then it would have been different to put the value of this n in this name. But if we have written a name here and we write a name here too, then there will be no effect of this here. So what we do is we write this keyword here. Okay. What does this mean? We are talking about this name.

  167. Speaker 1

    And you know this is a special keyword which points, which refers to the current object.

  168. Speaker 2

    Okay.

  169. Speaker 1

    So now see, we have made this a very special type of constructor. I said that if I just want to give a name value dynamically, then.

  170. Speaker 2

    It will call this constructor.

  171. Speaker 1

    But if I want to give a value of name and age, then let's do one more thing. Let's write another constructor for it. Here we write string name and int age.

  172. Speaker 2

    And let's do something like this. This.name equal to name. Name. And this.age equal to age. Something like this. So if you call this constructor, then you call name. and age. How will we call it? Nothing.

  173. Speaker 1

    We will make a reference variable of student s1. Now see, either I can give an empty, or I can pass a name here, then this constructor will be called. Or I can pass my age with the name, then this constructor.

  174. Speaker 2

    Will be called.

  175. Speaker 1

    Basically, as we overload the function, we have overloaded the constructor, right? We will do this later, now we will move forward. In the same way, what did I say? I make another constructor in which one can give name, age, and roll number. roll number. So in that case, I'll do this.roll number equal to roll number not Rohit roll number. I'll sign it like this.

  176. Speaker 2

    Right? Okay, very good.

  177. Speaker 1

    Now I'll make another construct in which you can give values of 4. If someone wants to give some values of 4 dynamically. So here I'll pass another string whose name will be college. The spelling of this string is wrong.

  178. Speaker 2

    String. Okay?

  179. Speaker 1

    And here what I'll do is this.college equal to college.

  180. Speaker 2

    Okay, college. Very good.

  181. Speaker 1

    So, I have made four constants. All the values, three values, two values, one value and zero values. So, now I can make any kind of object here. So, let's say I made an S1 which doesn't take any value.

  182. Speaker 2

    Okay. This is also valid. I can make an S2. Okay. I can also make an S2 here.

  183. Speaker 1

    Let's say it only takes a name. Let's say Aditya. Here, sorry, Aditya.

  184. Speaker 2

    Okay. I can make an S3 like this. Now you must have understood what I am doing.

  185. Speaker 1

    I am giving each one a value. With age, with Aditya, it can also take that. Let's say roll number 101. Let's do one thing. Let's give a new name here.

  186. Speaker 2

    Rohit. And let's give a new roll number. 102.

  187. Speaker 1

    Let's give another new object. With the name S4, what it does is We can also take a name, this time we will take Rohan. Age should be 28 only. Here let's say age is 30. And this time roll number is 103. We have taken the roll number. And I will make one more object, S5.

  188. Speaker 2

    S5.

  189. Speaker 1

    Which will take all the value. Let's make Rohan's O small. Let's call it Mohan. Let's say he is 31 years old. Role number is 104. And college is let's say IIT Bombay. So we have to make 5 arguments S1, S2, S3, S4, S5 And call different constructors With 0 parameters, 0 arguments 1 argument, 2, 3 and 4 arguments So we can do this Now you must be knowing If I print s1. name in this constructor Then it will print null Because default value will be printed Because there is no value here In fact name, age, role number or college I will try to print s1. anything in this All default values will be printed Name and college will be null Age and role number will be 0 We know that What will happen in this?

  190. Speaker 2

    S2.

  191. Speaker 1

    Name will be printed But other things like age, role number and college What will go? All this will go to default Your age will be 0, role number 0 and college again null In this case, in S3 you know that there is name and age Only role number and college will go to default In S4, only college will go to default And in S5, no value will go to default We have given all the values here So we call this constructor overloading Simple, we have seen this Now look, what can I do in constructor chaining? I can reduce this code a little bit. I know that here name will be assigned and all other default values will go.

  192. Speaker 2

    Right?

  193. Speaker 1

    Here name and age will be assigned and all other default values will go. Here name, age, number will be assigned and all other things will go. College is left, that will go default. And here no default will be left. So if we have 4-5 types of constructors exist. And final constructor is this. Which basically assigns everything. So, I can call other constructors using this keyword.

  194. Speaker 2

    What should I do?

  195. Speaker 1

    I'll write a syntax, this, parenthesis. I'll tell you what I want to do. And here I'll pass name. Because this is our name here. I will pass zero instead of age, which is the default value. And null, which is the default value of our college. Do you know what I did here? I called my biggest constructor from this constructor. By giving all the values. Name, age, roll number and college. And how did I call? Using this keyword. So what is the second use of this? It can call other constructors from one constructor. And what is its syntax? You write this keyword and then make a parenthesis. As if you are calling a function. So again, I said this is a special keyword. It has multiple uses. So if I go here, I will go straight here.

  196. Speaker 2

    How did I go? I didn't do anything.

  197. Speaker 1

    If I hovered on this and grabbed a command in my Mac, So here I got the icon to click on it.

  198. Speaker 2

    It's simple. It's in every IDE. You can go and see it directly. Which one I'm referring to. Okay. Very good.

  199. Speaker 1

    Now I can do the same here. By not assigning the name age like this, what should I do directly?

  200. Speaker 2

    Should I call this? Name instead of name.

  201. Speaker 1

    Age instead of age. Means I'm calling the biggest constructor.

  202. Speaker 2

    Okay.

  203. Speaker 1

    Now if there's no roll number, then zero. And if there's no college, then null. The default values that we have passed. We again put this as semicolon. Same here, I would have written such a big 3 lines, what would I have done better than this? I would have used this keyword, instead of name, I would have used name Instead of age, I would have used age, instead of roll number, I would have used roll number And here would have gone null Something like.

  204. Speaker 2

    This, here name would have gone, okay Right?

  205. Speaker 1

    And here we have already done it, here we have to write it once, we have to write it once and assign it to everyone Right?

  206. Speaker 2

    Now what will happen?

  207. Speaker 1

    Now look, nothing will happen with this, the same output will come which came earlier We just changed all the constructors Now this This constructor, this constructor, and this constructor. All three of them are calling this. So finally, the answer is coming from this constructor.

  208. Speaker 2

    Right?

  209. Speaker 1

    As soon as you get the name, what did it say? I am calling this big constructor. I will give name instead of name. Rest, look, I will give default values instead of age, role number, and quality. Here, default values came. In fact, if you want now, then you can use this default constructor also in the future. What you do is, this this.name, instead of this, let's say you take a default value. You don't want it to be null, you want it to be unknown on the screen That this human is still unknown You want to call this.h as 0 Because null values are not always good We'll see in the future how big the null pointer exception is in Java So we'll avoid null, 0 And let's say again instead of college I did unknown I want to give unknown instead of null So instead of calling this, I could have simply written this I would have passed unknown instead of name And you know what I am doing, I am calling bigger constructs I would have passed 0 here, 0 here and again unknown here Something like this So I don't need this line I could have written this line and run my work And semicolon here And I use the same procedure here I could have given unknown here instead of this college Something like this, right? So what am I doing now? I am calling my last constructor itself Now I would have given unknown here in the same way I would have given unknown here in the same way Now look, I didn't get any null values Wherever the null value could have come, instead of name and college, I passed an unknown variable there So this is called constructor chaining. If you see, by printing anything, the same output will come. Like if you will name S1 this time, then it will come unknown. If you will do college, then it will come unknown. Rest both will be age and number 00. Same if you will name S2, then it will come Aditya. Rest age and number 0 will be college.

  210. Speaker 2

    It will be unknown. In this, Rohit and age Name and age will.

  211. Speaker 1

    Be there, all other default values will be there College default will be there and nothing else will be there So let's do one thing, let's print a random value out.println I will try to print the role number of S3 So I should get 0 And if I try to print college of S3 So I should get unknown Now let me tell you one more thing, how did we do constructor chaining? We connected all these four constructors to this big constructor. There is one more way of constructor chaining.

  212. Speaker 2

    How?

  213. Speaker 1

    I connect this smallest constructor to a bigger constructor, connect this bigger constructor to this bigger constructor, connect this bigger constructor to this bigger constructor, and connect this bigger constructor to this bigger constructor.

  214. Speaker 2

    How? It's very simple.

  215. Speaker 1

    See, I know that the smallest constructor which doesn't take any argument, I don't give anything in it, I just give a name. Means I am calling this constructor because I have this name. Means this constructor will call. Which constructor will call? Whose signature will be given by this. Basically whose name will.

  216. Speaker 2

    Take only one argument.

  217. Speaker 1

    That is this constructor. See, if I go there, I will come to this. Now what will this constructor do? This constructor will call a new constructor. It will pass only the name that it got. And a new age will pass 0. And it will pass a bigger constructor than itself. Because this constructor calls two arguments. And who is this constructor with two arguments? Now what will it do? It will pass name, age, and role number to a bigger constructor than itself.

  218. Speaker 2

    I will erase this in this way. First of all, what happened?

  219. Speaker 1

    This constructor called a bigger constructor than this, gave unknown and got unknown in name. This constructor took unknown here and passed zero to the bigger constructor than himself. This constructor got name and age. He passed zero to the bigger constructor than himself. So he got name, age and roll number. He didn't get college name. So he passed college name as unknown to the bigger constructor than himself. So now see how I did the chaining. I connected this to this, connected this to this, connected this to this, and finally connected this big one to this big one, the biggest one, right?

  220. Speaker 2

    So this is also a way of constructor chaining. What will still be happening?

  221. Speaker 1

    Whenever I make an object like this, new student, empty, then first of all this constructor will call. As soon as it called, it called this constructor, it called this constructor, it called this constructor, it called this constructor. And finally, the values are the same. What would have happened here? It called unknown with this. It called unknown and zero with this. It called unknown with this. It called unknown with this. And finally, what are the values here? Unknown, zero, zero, unknown. So, if you don't believe me, then do one thing. Print the details of S1. Okay, let's print this once and see. Absolutely unknown.

  222. Speaker 2

    Zero, zero, unknown. What we had expected. And if you want to see that all the calls are like this. This call from this, this call from this, this call from this, this call from this. So let's do one thing. Let's print something in every constructor. System.out.println Okay. I am in first constructor. Let's write like this. Okay.

  223. Speaker 1

    Just to see that are we running in the same way. First this should come. Then this should come. We will call this. Okay, I'll tell you why there's an error here. We can't write this line here. We have to write this line after this. Okay, I'll tell you why now. Okay, we write this after this too.

  224. Speaker 2

    Okay.

  225. Speaker 1

    Chalo, saath hi badadata hu. Dekho, jab bhi aap koi keyword likhte ho na this, ye hamesha pehli line honi chahiye constructor ke andar ki. Agar ye line pehli nahi hai, iss se upar bhi kuch hai, to error aega. To ye line hamesha pehli line honi chahiye. Bas itna aapko yaad rakhna hai. Ye allow hi nahi karega java ki iss ke upar koi line ho. To isle hamara error aara tha. Ab isme again, main yaha pe likh lunga I am in let's say third constructor. Yaha pe likh lunga I am in 4th constructor and here I will write I am in 5th constructor something like this so see on my screen when I will try to print these 4 then for these 4 to be printed all these values will also be printed so first of all let's see what will be the output Let's see it by ourselves. First of all, we made its object student s1 equal to new student. As we made its object, first of all, this constructor was called. At the same time, as we wrote this new student keyword, this constructor was called.

  226. Speaker 2

    And what did it do?

  227. Speaker 1

    It called this big constructor. So this was called. Then it called the big one. This was called. It called the big one.

  228. Speaker 2

    This was called. It called the big one. Finally, this one was called.

  229. Speaker 1

    Then he assigned values to all of them, name, age, roll number, college Name got unknown, age and roll number got 0, college got unknown again After assigning these values, our screen will be shown as I am in 5th constructor Why will this come first? Because we talked about recursion as well See, in the same way, he called this, this line is not executed yet Because of this call, it reached this line directly Now what is the first line of this? As soon as this call happened, it reached this constructor.

  230. Speaker 2

    This line was not executed.

  231. Speaker 1

    Same, this reached this constructor, this reached this constructor. And it did all this work first and then printed I am in 5th constructor. Now this constructor is finished. Where will this call go back? The one who called this big constructor. This was called by this constructor. Then it will print on the screen, I am in 4th constructor. Now this is also finished. Now where will.

  232. Speaker 2

    The call go back?

  233. Speaker 1

    The one who called this, this called this. Now it will print I am in 3rd constructor. Then it will go back to this, I am in second. Then it will go back to this, I am in first. And we will come out of all these.

  234. Speaker 2

    Right?

  235. Speaker 1

    After that, when we will try to print these things. Then our unknown, zero, zero, unknown will be called.

  236. Speaker 2

    Okay?

  237. Speaker 1

    So, first it should come, I am in fifth constructor. Then fourth, third, second, first. First, all this should come. After that, all these values should come.

  238. Speaker 2

    Unknown, zero, zero, unknown. Okay? Let's see, is this happening?

  239. Speaker 1

    Look at this, I am in 5th constructor, 4th, 3rd, 2nd, 1st Why is it printing again and again?

  240. Speaker 2

    Because we have.

  241. Speaker 1

    Made a lot of objects, let's cut them Let's comment them, sorry Now let's see Look at this, I am in 5th, 4th, 3rd, 2nd, 1st constructor And after that all the values are printed, unknown, 0, 0, unknown So with this you can see the flow of control, how it is going First from here, here, here, here, here, here And then it goes back to the top and prints all these values.

  242. Speaker 2

    So this is Constructor Chaining. You can play with it more. If you want, you can call this before this.

  243. Speaker 1

    And then it will print I am in 5th Constructor. And then it will print I am in 1st Constructor. Same you can play with everything You will understand how constructor chaining works And it becomes very important When you have to do something like this That you don't have to write a lot of long code Because there are only four variables here You want this to get its value by calling this This to get its value by calling this This to get its value by calling this So we have chained all our constructors together Using this keyword So this is a very important keyword You have to remember What does this always do? It stores the reference of the current object. So you can write this.name like this or you can call a constructor using this keyword.

  244. Speaker 2

    Okay? Let's go.

  245. Speaker 1

    I hope you understood all these things. Now let's conclude this lecture. Before that, I want to tell you one more thing. This is a very important question in an interview. Can we manually call a constructor?

  246. Speaker 2

    Manually.

  247. Speaker 1

    Like we call other functions and its answer is no. We can't just manually call the constructor. When will the constructor be called? When its object is made here, like student s1 equal to new student. Here we call the constructor for the first time. Or using this keyword, which we had written in the constructor. What did we do? We didn't write anything like this. This name and let's say here comma age. And we asked them to call this constructor. So we can do it this way too. But we can't write like a normal function in the middle of the code. That I want to call a student's constructor. Or if I want to give an argument, I have to call it with an argument. This is not allowed. You can't manually call the constructor. It can only be called when you are creating an object for the first time or using this keyword which we just saw.

  248. Speaker 2

    So this is a very important question of the interview.

  249. Speaker 1

    You have to remember one thing. Secondly, there is another interview specific question which is very important. We had said that Yes, let's see here that whenever we write this line, student s1 equal to new student. So this new keyword, it dynamically allocates the memory. Dynamically means on runtime.

  250. Speaker 2

    Right? So it can also happen that on runtime, let's say this is our heap memory. Okay? Heap.

  251. Speaker 1

    On runtime, it didn't get so much space to allocate the object. It can happen. Because it is doing it on runtime, it can happen that our heap memory is already full. we don't get the space to allocate this object.

  252. Speaker 2

    So, this can happen.

  253. Speaker 1

    In that case, we will get a run-time exception.

  254. Speaker 2

    Run-time exception. I will write it properly.

  255. Speaker 1

    We will get a run-time exception when we don't have enough space in the heap memory. If the heap memory is full. Not enough space available. Not enough space.

  256. Speaker 2

    In heap. Something like this. Okay.

  257. Speaker 1

    So this is a very important question. Many times we ask this in interviews. People think that memory will be found every time.

  258. Speaker 2

    It's not necessary.

  259. Speaker 1

    There is a high chance that memory is found every time. But sometimes insufficient memory can be found. If your heap gets full. Second, as I told you. You can't call Constructor directly from yourself. You can call in two ways. Either when you are making an object for the first time. Or using this keyword which we saw earlier. So I hope you enjoyed it. You must have understood everything. That's all in this video. See you in the next video. Till then. Thank you so much.