7.15 Count Number of Objects of Class in Java

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so welcome back aliases and I've enjoyed different political earnings and in this video we'll talk about one of the most important question no you cannot be much in what's important question but this question is frequently asked to introduce so if you're going for jarring to you also going for any Java rated technologies maybe Android or maybe for the technologies they might ask you this question and the question is how will you know that how many objects of a class is created I mean what it means now so let's say you have you have a class here and let's say we have a class and the name of this class is a because I love this character a and in this in this class we have a Metroid okay so let's say we have a meter which is public voice counter okay now this quantity responsibly took print the number of objects created by so if I create one object of class a okay if I played only one object of class a this counter should print one if I create two of these subclass a this counter should print two if I've 2500 if I say if I if I create ten objects of this class a discounter should print ten right now normally how we create object so we'll be having so with this will be also having a main method right so let's say in this if you have a main method here okay and it takes some parameters again into string arguments right and inside this mean okay so let's say I paid one object normally how we create object we say simply a obj1 equal to new a so that's one object right if i can add one more object if i say a obj to equal to nu e so if i'm creating two methods right so so it's I'm getting two objects right so this context should bring - so how exactly will be doing this but let's say if I if I create one more object which is three and if I say new a thing to does be happening object and you'll knife I say obj 3 dot dot counter so if I if I call this method which is counter okay so that's that matter if I call this method so it should bring the number of objects which are created how to do that so we never say we want to count so anyway you say count of course we require a variable wise which will hold the all of the counts so which whatever variable it will be writing here okay so let's let's try it out so if I say I have a variable which is int I okay so I have a variable which is int I here and this I responsible to count but hold on how would you I know that you are getting the object it is going this way every time you create an object okay so every time you feed an object you call one method right and that matter is constructed of course so what if if you can simply create a constructor here will that work if I pay the constructor but what's next so every time you create an object it calls the constructor what if in this constructor I can simply say I plus plus so what will happen now is every time you create an object it will increment this value and the default value for this I will be 0 right so initially it will be 0 but the problem is and then not the problem but let me print the value of I here so what will happen now is if I create the first object okay for this object will be having a book and which will be having a variable I and party for the value of I will be 0 right and then you're saying I plus plus so it becomes 1 so we got 1 object that's great right it is counting and then if I if I don't create this object if I say obj1 dot counter of course the output will be 1 why they got one object but what if I create the second object it will again create a new object and the default value for I I can do this video because this is instance variables right and what is instance variable every instance will have a copy of it own right so when you say you have an instance variable every object will have a copy of it and then you say copy there are two different values right so this object will have a different value this will have a different value and this will now become one again because initial value is 0 and then you are saying I just does it will make it 1 so you have two objects but still the counter is 1 I'm going to enjoy discharge line it will create a new object with the value which is one again where it is counting with what we want as a number of this increases so as a number of objects increases what we want is we want it to count but the problem is this is instance okay so what what if we'll let's not take instance variable what if you take class variable what is class variable now so class variable is a variable which is shared amongst the object so if one objects changes the value it was effect on the second object if second object is evaluate with a pictorial third object that means this that variable does not depend on objects that depend upon the club I mean they develop laws that's what is called a class variable and how to create a class variable by using a keyboard colors panting now what will happen is every time you create object initially the value of I will be zero of course so when you create the first object the value of I will not be will not belongs to objects now because that's not an instance variable that's a static variable so somewhere in your stack memory okay metal area basically in that you will be having a video which is I which belongs to class now and when you say initial value is zero you are making I plus plus so it becomes one now you're getting second object okay because this only nodes one so whenever in second object you it will not load again because we have a variable there so it will make just make it two because we didn't press plus for this object it will make it three right because it is plus them and then when you're calling obj any object may be obj three dots counter it will spring three even if you're printing or beach a one dot counter the output will be three okay so you can just try it out this example on your laptop maybe text off so if you are using Eclipse net which doesn't matter this code will walk everywhere okay so this is this is something the major be asking in the interview so you can just write out and next time when you go for the interviews do answer directly okay so yeah that's it from this video if you want some more interview questions do come it in the comment section you can actually submit some more questions there okay if you have if you have lots of questions with you you can just submit those questions there so I can also and that's it do subscribe because I want most of scribes but more views thank you so much watching
Info
Channel: Telusko
Views: 91,045
Rating: 4.9321032 out of 5
Keywords: navin, reddy, tutorial, java, telusko, learning, objects, class, interview, question, count, how to, simple, basic, use
Id: zZlqRkgNSrg
Channel Id: undefined
Length: 6min 48sec (408 seconds)
Published: Thu May 12 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.