#4.3 Java Tutorial | Constructor Example

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Oh [Music] so in this video we'll try to explore more about this stuff here so you can see this calc and the round brackets now if you can observe closely this name is same as the class name right and it has the round brackets and whenever you use a round bracket it simply means a method right so it looks like a method but it has the same name as the class name so that is weird right so this thing here it is called as constructor so whenever you create an object so whenever you create an object by saying new calc that calc and bracket is a constructor and construct is a very important thing when you are getting the object now how that works so just to define that let me remove this method which is perform let me just clean the code a bit for you let me remove all this stuff so what we have we have this three variables here and now this int result is only for the fetching the output right let me remove this so this is your constructor now how do we define a constructor so we have to say calc obj equal a new calc so when you say this calc it will call the constructor but holdout it is calling a constructor and we don't have the constructor in our class how it is doing that so what happens is Java says okay see we know that whenever you create an object you have to call a constructor then why you need to create a constructor I will give it for you so by default Java gives you the constructor for calc the constructor with Java gives you only does one thing it allocates the memory that said nothing else so the constructor which is given by in Java world it is only for allocating the memory what I want is I want a constructor who will assign the value so let's say I want to assign the initial value of num1 and num2 as 5 and 5 what the value should be 5 because by default the value is 0 right example if I try to print the value of num1 if i say system dot out dot println obj dot now 1 if i try to print that the output it is giving me is 0 right I don't want 0 I want the default value to be 5 so what you can do is you can define the constructor by yourself so you can say public calc that's how you can define your own constructor now point should remember our constructor has the same name as class name that's very important second doctor never returns anything and that's why you cannot write a written type there we can't specify and you can specify float even if you specify that you know it will not give you a letter but it will not be a constructor now so if you want to have that feature of constructor we should not write any written type there so point remember constructor name should be same as class name and second it doesn't return any value the third point is we don't have to call the constructor it will be called automatically when the object is created so as soon as you say new calc it will create the object for you plus it will also call the constructor so there these are three important points for the constructor first one constructor need to have the same name as class name second constructor doesn't return any value third whenever you create the object constructor will be called automatically so whatever doing this constructor is I will I want to say num1 equal to let's say five and for both the variables in fact for num1 and plus num2 or num2 also i want to assign the value which is five and now if you get on this code you can see we are getting the output as five so the value of num1 is five it's because when you call the constructor here it is getting called and it is assigning the value of 5 2 num1 and we are printing the value here it is that simple we can also print some statements here let me say in constructor so you can see we don't have to call the constructor it will be getting called automatically so if I run this code now you can see we got the output as in constructor and then we are printing the value as 5 so it will first call the constructor then it will print the value so that is how you create a constructor we have some more things with the constructor in fact we can create a constructor we can have to construct in the same class example we can say public Cal so we can have to construct in the same class provided they have a different signature so you can see it is giving you error now because you cannot have two constructors here I'm going to be the same signature we have to change the signature now when you say change signature does that means change the name that will not work right if you if you change the weight of constructor name then it's no more constructor because concetration have should have a same name as class name but you can change the parameters here so you can say n so you can see we have two constructors one which is not taking any parameter and second which is taking the parameter so now we have two constructors with different signature you can say one is without parameters and one is with parameter now what we can do is we can say num1 equal to n and even num2 is equal to n so instead of assigning a value 5 so you can see ensure of assigning a value 5 we can pass any value here we can pass 7 now that value 7 will be assigned to n now since you are so while creating the object if you pass a value it will call that particular constructor which takes an int value and will assign that n value to num1 and num2 and i feel on this code you can see we are getting the output as 7 not 5 that's how you can pass the value but it's I don't want to pass a int value I want to pass for 7.5 how that works now when you are passing 7.5 of course you cannot accept that in int right and that's why it is giving you error so we have to create one more constructor which is public calc which takes a double value which is double D again we can go for any variable name doesn't matter and we can say num1 equal to but again no one is an integer right we can again cast it right we can crash through the value of double the important thing is not the value assigning is the important thing is you can pass any parameter so if it's passing specific but I'm into like 5 7 point 5 which is double you have to accept that bit double or when you are passing a string you can accept musing string that is how you create a constructor so we can have multiple constructors but make sure that every constructor have a different signature if you run this code now you can see we are getting the value as 7 only because we are passing 7.5 let's see if I pass nine point five and if I'm this code it will get casted and you will get only 9 in fact we can also pass two parameters so we have the first one as double second is int so we can assign the int value to num2 and for that we have to pass two values now what is nine point five and let's say we are passing eight also so you can so if you are passing multiple values you have to accept multiple values it's just that you have to match the signature so the number of variables you are passing here you just have to match the signature here so we can have multiple constructors with different signatures so this one is for default constructor because it is it does not have any parameter now why is for because even if you don't mention this constructor I mean if you don't have any constructor by default you're J your system will give you one constructor that's what is called as default constructor and the other constituents are called as parameterised constructor again we'll talk about constructor more when we when we moved towards inheritance concept but time being this is what you have to remember about constructors so that's it that's it about constructors in this video see you in the next video
Info
Channel: Telusko
Views: 156,485
Rating: undefined out of 5
Keywords: telusko, navin, reddy, tutorial, java, constructor, example, create, default, assign, value, parameter, parameterized, overloading
Id: lrIlVcsZrx0
Channel Id: undefined
Length: 7min 12sec (432 seconds)
Published: Mon Jan 22 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.