#4.2 Constructor in Java Theory

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back alias this is I've been ready from Paris colonics and in this video we'll talk about constructor so whenever you talk about Java so Java specifically follows object-oriented right so now in Java 8 we have functional programming so if we talk about this concept of object-oriented we have certain certain words like we have object we have class and we also have a trombonist constructor so where the use constructor so let's say when you have when you have a plus let's say this is no class here which is class a and in this class we have certain variables so let's say we variable we have is int I and then we have one more variable let's say not int but this family variable is float okay we have float let's say F now if you have two videos in this class right and let me just end this class here so if I ask you what's the size of I and the answer is four right so also for you so in takes four bytes if I ask you about float again float takes four bytes so eight total we have eight bytes right now let's say I want to gate object of a here so when I want to create object of a so let's say this is our main function PS v m now why exactly we require PS vm so let's say let me take my duster okay so why we have this PS VM so let's say if you want to create the object of a we require a main method why because the execution of your code starts from main so what we need so in order to call a main method so main method takes a parameter right so maybe because if you want to pass some initial parameters you will pass those parameters use which is string Arg use and since the type of parameters can be anything it can be int it can be float it can be string so that's why we take string because string accept all the values then we are not sure that how many values our user will send one or two so you are taking an array now this mean will return nothing so we have to write void because it will return nothing but a chance that this main method can be static method now why should be static is because to call any method in Java we require object right and you can create object once your execution starts main is the starting point of the execution so you cannot you cannot call main without object and you cannot get object without main so that's a deadlock there to solve the deadlock they are just saying that this is a static method that means in order to call main you don't require an object you just require a class name and we'll make it public so that is public static void mean okay so we are going for all this and so we have prosthetic void mean if I open this and close this now how to pay it object of a so when you want to get object of a we can simply say a and then we say obj right so that's how you create object in C or C++ but in Java in order to create object we have to provide one more step so this is your reference creation so this is reference in order to create object we have to specify a keyword which is new so we use new to create the object of a class in fact it provides this space because if you talk about object so it will take some space inside a heap memory so there will be a heap memory so in order if you know how to create object of inside heap memory we require activities new keyword so new keyword will consume this space right so just explaining example just imagine you want to travel you want to travel and again we have seen this and one of the video which is object instantiation whenever you want to travel in train you buy that pillow right which is which will not have in air so you have to inject other or you have to blow some air so you have to blow the air with the above new keyword that will provide the memory but how much memory you have to also specify the amount of memory right so let's say if you want to create a variable 8 so you look you PI's 4 bytes float 4 bytes but what about the object how much space is required for an object so this answer will be given by the constructor so every class even if you don't pension even if you don't mention there will be a constructor in in every class so constructor is like a member method which means every method will have brackets so we have a bracket here okay this thing is a constructor yes this is a constructor okay so a bracket so this bracket here this is a method ok so black it means method and so constructor is a member method which has the same name as class name so you can see class name is a that's why constructor name is also a ok so that for the first rule or the first thing about constructor constructor is a member method that means we require round brackets second constructor will have the same name as class name so that's why we have a next constructor will never return anything that's why we have to write public so no need to mention any written type here no void no int and that's your constructor okay so every time you want to create an object you have to use constructor even if you don't define the constructor it will be there inside your class and that's why these constructors are called as default constructor because you are even if you don't create the constructor it will be that that's why just call it default okay so now here okay just me just do some line so here you have to mention the constructors so that every time you want to create the object it will communicate with your JVM that I want to create the object how much space I require that will be given by the constructor so the use of constructor it will be used to allocate the memory to the object okay so let me repeat or this constructor it's a member method okay write it somewhere but right okay so we'll use this space so constructor is a member method second it has the same name so same name as class name the third thing is it will never return anything and the fourth thing is it will be used to allocate memory it will be used to allocate memory okay now you can also use the constructor to initialize your value so let's say the value of I by default will be let's say zero and it will be zero point zero it has a default values for int and float but let's say I want to provide the default values for the variables not zero but let's say five so I can say in the constructor I equal to five and let's say for if I want to specify five point five so the value of is five the value for F is five point five right so we can provide the initial values of the variables in constructor so that's the one that's one of the advantage of constructor now question arise to assign this values we have to call construct right so we have to call the constructor and the amazing thing is you cannot call the constructor for the same instance because every time you create an object it will call the constructor right so as soon as you create the object it will call this block you don't have to call the constructor it will be called automatically sounds good so that's how you can use constructor but we can also have different types of constructor so we can have here so we have we can have one more constructor in the class so we can say public a but this constructor will have a different parameters so we can specify int I and we can specify a lick will say this is int K so we can specify I equal to K here right so we can have two constructors in the same class provided they have different parameters okay and this concept of having multiple constructors with different parameters the same class is called as constructor overloading against a part of polymorphism right so once you know about constructor once you know about method overloading so just combine it it is constructor overloading guess is good now in order to call this block so if I send it let's say if I printing something here if I printing something that says system dot out dot println it is hi and Here I am printing system to ordered println hello so if I run this code now it will print hi it's because we are we are calling a default constructor but in order to call the second constructor which is have to pass a value let's say if I pass six so in this a bracket if I pass six it will call the second constructor okay so let me just check my videos walking is it recording okay so so we have a constructor here in which you have I equal to five so if I run this code now it will print hello quite simple right now what next mm something more we can do here okay that's it that's your constructor so we can call the constrain this way and nothing much now let's say can we have mocha suta we can also have one more constructor here which takes two parameters one is a and it can say int K comma float J right so now in this case you have to pass two parameters so in this you have to pass 6 comma let's say 5.6 so when you pass this two parameters you have to also provide F here when you pass those two parameters it will call this constructor okay so that's how your constructor overloading works so to understand it with practically we have the next video we have talked about the constructor in the practical way so if we can run those codes there so so that's it that's it from the constructor so once you remember constructor is a member method it has the same name as the class name and it will never return anything and it is used to allocate memory it is also used to initialize your variables and that's it so that's it from this video so thanks for watching and do subscribe for further videos
Info
Channel: Telusko
Views: 341,826
Rating: 4.9265366 out of 5
Keywords: Constructor in Java, Memory Allocation using Constructor, Object Creation, Java tutorial for Beginners, java constructor tutorial, theory, constructor, what is constructor in java, java tutorial, class, method with same class name, telusko, learnings, java, navin reddy, navin, reddy
Id: YqBVgUh64Ks
Channel Id: undefined
Length: 10min 54sec (654 seconds)
Published: Sun Mar 27 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.