The final keyword in Java is used for restriction purposes and can be applied to variables (making them constants that cannot be changed), methods (preventing them from being overridden), and classes (preventing them from being inherited). Unlike private methods which cannot be inherited at all, final methods can be inherited but not overridden. Java's garbage collector is a demon thread that runs automatically in the background to destroy unused or unreferenced objects from heap memory using the Mark and Sweep algorithm. An object becomes eligible for garbage collection when its reference is nullified, assigned to another reference, or when it was created inside a method that completes execution.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
UnderStanding Final KeyWord Day 55 By kishanAdded:
right bringing it on ruining ice cream ingredient can you hear me can you see the screen can you please respond in the chat yes yes yes yes very very raining guys very very good evening all right so let us start our session here today so guys before starting our sessions as always let us try to have a very very small and a quick recap of the concepts which we had covered in our last class that is in our Edge test class so guys actually Yesterday by yesterday we have completed discussing about exception handling like we have discussed what is like an error what is the difference between another and an exception how many types of errors are there how many types of exceptions are there like what are if you want to handle an exception what are the blocks we need to use basically what is actually an exception handling techniques what are the important uh key points which you need to remember in exceptionally all all these special Concepts we had covered guys and what is the difference between throw through those keyword by HD we have already covered that particular concept now today guys we are going to start with one more New Concept entirely A New Concept that is understanding final understanding understanding final keyword understanding final keyword okay right understanding final keyword now guys before starting my session uh let me tell you one thing guys tomorrow we will not be having any session okay tomorrow is the holiday for you like it's a break why because tomorrow there is uh this Festival is there in Telugu states it is known as ugadi and uh in North India I mean some states they are going to celebrate it as uh Guru padwa right yes I mean both I mean um same day so that's the reason okay so tomorrow is a holiday guys I mean you can take a break okay obviously Day After Tomorrow is holiday holiday Sunday so after today's session we'll meet again on Monday guys okay we'll meet again on Monday okay so Saturday and Sunday there is no class for you like take a break enjoy the weekend guys okay all right all right so yes let us start our session here today so yeah and thank you guys uh yeah understanding final keyword right right so guys before going to final keyword like before discussing about final keyword I believe you know about like a static keyword like uh in how many ways we can use the static keyword guys in how many ways we can use a static keyword come on respond respond exactly three ways I mean we can use a static keyword in previous what are those like in which scenarios we can use yeah exactly exactly a variable a variable a method and a block right yes yes a variable can be declared as the static a method can be required static and a block can be declared as static guys just like that just like that when you are speaking about final also we can use the final in three ways okay we can use Final in previous any ideas any suggestions what are those what is the first one first one a variable can be declared as final second one second one yes a method can be declared as final and finally last one is last one is class class guys class class okay a variable can be declared as final a method can be declared as final and a class can be declared as final not block not block okay so kindly please don't get uh confused here guys okay I hope you're understanding me variable variable method and a variable method and a class guys class class okay right right right now guys variable if you are declaring a variable as final if you are declaring a variable as final final variables are constants okay if a final variables r constants constants in the sense once we have declared a final variable like once we have declared a variable as final we cannot change the values of those final variables okay final variables are constants now method when you are speaking about a final method guys final methods final methods can't be final methods can't be overridden final methods can't be overridden means we cannot override a final method once again I'm saying you we cannot override a final method okay now if you are taking final class I mean if you are making our class as final final classes final classes can't be final classes can't be inherited final classes can't be inherited as simple as these guys are simplistics okay so final we can use Final in three ways a variable can be declared as final a method can be declared as final and a class can be declared as final guys do not actually what is the use of this final keyword you know it is used to it is used to restrict the restrict the user actually final keyword is used for restriction guys do you remember what is the use of static keyword why we will be using static keyword I told you a point did you remember that why we will be using a static keyword anyone why I told you static keyword is mainly used refer very good very good Krishna very good static keyword is mainly user for memory management memory management later on guys I told you I told you yes exactly exactly static keyword is mainly used for memory management final keyword is mainly used for restriction purpose guys okay now let us try to have an idea about this final keyword okay let me try to have a class okay I am having a method in ta equal to 10 okay it's available and just let me try to have a method void method one void method one inside this method one I am writing system dot out dot println simply I am writing a class the method one called okay class a method one call like this and now system dot out dot println a plus plus and the system.out.interline now let me go to my main method okay uh main method now inside my main method inside my main method uh can you please tell me like a new class a DOT method one ah tell me try to give me a values like a class a method called not required just try to give me the output what is my expected output here and here very good very good go on go on go on snehal no wrong wrong body okay guys please observe here yeah I mean this is which increment pre-increment or post increment yes tell me this is post increment guys this is not pre-increment right so before incrementing what is a value a value is 10 only so 10 will be printed after accessing a then you are incrementing right so right now here a value is 10 here a value is 11.
like that so we will be getting our output as right click run as Java application and and then 11. okay guys come on here we have already covered this concept very starting days only come on come on come on practice practice okay now now let's please observe here uh please observe here now for example if I'm making this variable as final Did You observe like we are getting an error at line number 10.
we are getting an error at line number 10 why why we are getting an error at line number 10 we are getting an error at line number 10 because final variables final variables are constants we cannot change them we cannot change them so that is only reason this will be like a trump file time error okay because because final variables are constants we cannot change them we cannot change the value of final variable okay guys okay fine fine no issues now um okay guys what I want to do you know I mean here my output will be 10 right now right click run as Java application right my output distance now what I want to do now I want to perform method overriding for method one can you help me I want to perform method overriding tell me what I need to do first very good Mukesh you are saying to create a class okay so just let me try to take a class here let me go with like Class B okay guys I believe you have remember what is actually method overriding what is Method overriding writing two are more methods in two different classes so the Second Step means I mean what we need to do here guys we need to perform which concept inheritance inheritance very good okay so EFT and DSA extensor Class C like that okay uh so avoid method one and here simply I am trying to write system.out.intellent uh method over again over ryddem method over written now here okay main method guys by the way can you please help me out please give me the code I mean inside the main method can you please give me the code I want to prove that I am overriding give me the code I want to prove once again I'm saying you I want to prove that I am implementing method overriding I told you which code we need to write inheritance I told you four points in inheritance in those four points I requested you to go with the second Point second Point what is second Point guys what is second point please hold lucky child class object child class object with a child class object with a parent class reference Class A like this parent class reference once again please hold please hold child class object with a parent class reference okay now with the help of that reference with the help of that reference what I told you so right click run as Java application and of course we will be getting our output that is Method over reading I mean that is actually method overriding yes or no guys did you remember that guys please tell me here please tell me yes yes now what I want to say is what I want to do is for example if I am making this method as fiennial final please observe Class B nice please observe Class B why I am getting an error at line number five I am getting an error at line number five why because why I'm getting another line number five because final methods final methods can't be over r i d d e n final methods can't be overridden so guys what do you think can I consider final methods and private methods are same can I override private methods can I override private methods no I cannot overwrite private methods can I override final methods obviously no I cannot so can I think that private methods and final methods are same 100 percent no 100 percent no then what is the difference between a private method and a final method guys this is one of the most popular interview point of equation what is the difference between a private method and a final method okay guys now this is actually final method right okay final method okay guys method two right now am I performing method overwriting am I performing method already no I'm not performing method overriding now for example guys just let me try to keep these two lines in comment for example new class B new class B Dot method one can I write like this guys new class B dot method guys please observe please observe method one is which method like method One belongs to which class method one belongs to class A Class A and whenever you are performing inheritance guys inheritance here whenever you are performing inheritance can change the class axis parent class methods I mean can child class object access parent class methods of course hundred percent hundred percent that is exactly what we are trying to do so will final methods participate in inheritance will final methods participate in inheritance fine methods participating inheritance for example private private method if I am writing private method can I inherit no no guys we cannot did you understand the difference between a private method and a final method guys please tell me right right now now just let me try to write it like a small Point here guys very very small point private method private method we cannot inherit private method so we cannot perform method overriding private method guys understanding me we cannot inherit a private method so we cannot perform method overriding final method we can inherit final method but we cannot perform method overriding okay we can inherit a final method but we cannot perform method override do you understand the difference between a private method and a final method yes that is good that is good that is good guys so here we are I mean we'll be getting an error I will just let me try to make it as final like this if you are making this as final then we will be getting an error here we'll be getting an error here why because final methods final methods can't be overridden final methods can't be overridden now class guys class for example if I'm making this class as final immediately I am getting an error here can you see this exactly exactly final classes can't be inherited final classes can't be inherited that's what I told you guys final keyword is mainly used to restrict the user okay do me a favor guys can you please have this program for me both both the programs please guys please inform if it is completed please inform me if it is completed don't say title don't say title inform me I'm waiting right I hope it's come here Class B guys Class B Class B did you note on these comments also comments comments also okay okay please Class B no guys just one minute one minute one minute yes one minute one minute please Guys these comments are also important please one minute one more minute okay uh alpesh you're asking me questions sir can final methods can be overloaded guys anyone wants to answer can we overload final methods anyone wants to answer can we overload final methods Krishna yes Mukesh yes rupali no I told you any method can be overloaded any method any method can be overloaded finding whether private mother static method abstract method protected with a default method that does not matter any method we can overload don't forget don't forget the basic okay I hope it's completed now Class B Class B Class B guys here we will be getting an error here also will be getting an error guys please don't get confused in Java you don't have constant keyword okay we are having a keyword known as const c o n s t but that is actually a reserved keyword as of now we are not using them I mean we are not using that there are two reserved keywords are there in Java I told you did you remember them go to end c o n s t those are reserved keywords but we are not using them in Java as of now foreign all right guys I hope it's completed I hope it's completed for everyone now now right let me remove this all right guys our next topic is garbage collector let us try to have an idea about garbage collector okay just a second foreign did you remember when we had covered an idea about garbage collector I mean we have already discussed about garbage collector did you remember when yes yes we have discussed about garbage collector whenever we are dealing with whenever we are dealing with objective class object class right okay now understanding understanding garbage collector garbage collector okay now now first point the very first point which you need to remember in garbage collector is guys garbage collector is a garbage collector is a demon thread okay just let me try to write a small point [Music] small point garbage collector garbage collector is a demon thread okay it will be executing in the background of every Java program foreign executing in the background of every Java program okay every Java program no need to call the garbage collector manually but if we want to call the garbage collector also we can do that with the help of system class and runtime class okay no need to call the garbage collector manually but if you want to call the garbage collector I mean if you want to call the garbage Carter also we can do that manually manually also we can do that with the help of system class so system class with the help of system class and runtime class these are predefined classes which are present in Java okay we can call it we can call it now garbage collector will use us an algorithm that is Mark and sweep algorithm mark and sweep algorithm so what garbage collector will be doing you know garbage collector will Mark all the live objects all the live objects and it will destroy all unused nor unreferenced objects as simple as this now basically guys what is the use of this garbage collector what is the use of this garbage cut we have already covered that we have covered this particular Point what is the use of garbage collector it is used to destroy all unused or unreferenced objects from the Heap memory guys did you remember that do you remember that I don't like we already discussed that I mean basically what is use of garbage character it is used to destroy all the it is used to destroy all the unused unused or unreferenced unreferenced objects from the Heap memory guys okay so basically garbage collector is a a demon thread it is a demon thread now what do you mean by demon thread demon thread means it is a thread which will be working in the background of a program no need to call that thread okay automatically it will be executing it without our presence okay without our involvement demon threats they will be getting executed automatically so likewise garbage collector will be working in the background of every Java program no need to call it manually but if you want it manually like if you want to call it manually also we can do that with the help of two classes what are those guys what are those two classes first one is system class and second one is runtime class runtime class now here comes a question question guys question question is when that's like a question okay question is when an object will be when when an object will be eligible for garbage collection when an object will be eligible for garbage collection actually this is also an important point of view like an important Intel point of view question guys when an object will be eligible for garbage collection so guys actually an object will be eligible for garbage collection in any of the three below mentioned previous one two three an object will be n object will be eligible eligible for garbage collection in any of the below mentioned ways first one first one that is if we are nullifying the nullifying the reference second one second one if we are assigning the reference reference to another reference if we are assigning the reference to another reference finally last way is all the objects all the objects which are all the objects which are created inside a all the objects which were created which were which were created inside a method in these three scenarios an object will be destroyed guys okay now did you remember what is the use of finalized method did you remember that particular Point guys uh guys you need to help me out you need to help me out I told you that point also did you remember that what is the use of finalize method if garbage collector is acting on our program if garbage collector is acting on our program it will internally call the finalize method finalize method will close the connections with that object do you remember this point guys finalize method finalize method will close the existing [Music] existing connections with that object guys we have discussed this point here we have discussed this literally we have discussed this point like whenever we are doing our object class methods we had covered this particular point right yes now let us try to have an idea here guys let me just try to have a program okay now here okay I'm writing a method void method one m-e-t-h1 void method one and here and here uh just let me try to have a small statement system.out.printle and Method one called method one called and uh okay let me have my main method now inside my main method inside my main method what I'll be trying to do I'll be trying to write uh I'll be trying to create two objects Class A like this okay two objects a o b j a o b j one a ob J1 belongs to class A like that and uh a ob J 2 a obj2 like this okay so how many objects okay Mukesh you're saying sir it was not discussed you said it will be discussed during exceptionally what no guys Mukesh what I am trying to say is whenever we discussed about finalize method did I mention you that finalize method will be internally called by the garbage collector yes did I mention you that particular Point Mukesh that is what I want to ask guys did I mention that point like yes please uh help me out I I think I had mentioned that that is actually an important point now guys in object in object class in our object class we are having different kinds of methods are there right in object class like what are those uh two string method dot equals method finalize method I told you we will be discussing about Cologne method clone clone method I told you we'll be discussing about clone method after exceptional handling yes uh guys please help me out here yes yes I told you we will discussed about clone method but not about finalize finalize method at that time only I told you come on yeah right right right and of course we'll be discussing about the Clone method also I promise you that right okay so guys come back let us come back let us come back here now here okay uh I'm having Class A in my class A I'm having a method and inside the method I'm having a main method fine now inside this main method I'm having two objects basically two objects okay fine now what I want to do now let me just try to call a method guys a o b j one dot a method on like this now right click run as Java application I'll be able to get my output that is Method one called okay now I want to call the garbage character sir you said no need to call the garbage collector garbage collector will be working in the background of every Java program uh yes hundred percent no need to call the garbage collector but I already told you one more point that is garbage collector is a thread I told you did you remember that demon thread we cannot guess the output like when a thread will be starting can we guess that we don't know that we don't know that so that is the reason I am trying to start the garbage collector manually by using system dot GC method now GC is a static method which is present inside system class as GC is a static method you guys know that we can call static methods with the help of come on with the help of class name yes yes we can call static method of the help of class name now let me execute my program guys right click run as Java application I'll be getting like this now is garbage collector acting on my program or not how do I know that how do I know that is there if there is a way uh I mean that would be better am I right or wrong if there is a way that would be better right yes so exactly so what I want to do here you know I want to write I want to write finalize method let me override finalize method okay p r o t e c t d protected void f i n a l i z e finalize protected void finalize and here system dot outdoor println I am writing um garbage garbage garbage has been civil elicitly collected like that and now if I execute my program right click run as Java application I did not got my output so garbage collection is not at called we'll do one thing guys we'll do one thing let me try to nullify the reference UOB J2 equal to a obj one okay sorry first let me try to nullify the reference a obj1 equal to null nullifying reference means nothing simply making this difference point to null that's it guys not more than that okay guys are you understanding me please tell me please tell me yes Ganesh garbage collector is a demon thread the name of the thread is demon thread it is a special name uh Ganesh right the rules of like what is the use of beam methods you know demon thread means it will be working in the background of every Java program right yes now guys please observe here please observe here now I had made this object a reference as null now let me execute my program right click run as Java application is garbage collector acting on my program is garbage character acting on our program yes when garbage collector is acting on our program if we are nullifying reference means actually for this object you are having this reference once you pointed this reference to null then this object will be unreferenced object you got it guys that object will be unreferenced object so that is the only reason it will be destroyed this is the first case foreign your reference to another reference are you understanding me guys now please check this right click run as Java application again garbage has been collected why because right now A will be J1 and aob jet2 both are pointing towards the second object this is first object and this is second object right both both these references are pointing towards this object only so that is the only reason for this object there is no reference automatically that will be destroyed from the Heap memory okay this is first and this is second if we are assigning the reference to another reference if we are assigning the reference to another reference for example guys I'm having a small doubt here simple Doubt Simple doubt for example right now right now please observe your guys please observe here B J One Dot method one instead of writing here instead of writing here if I write a obj one dot method one am I going to get my output fantastic I'm impressed guys very good very good very nice very nice I'll not be getting my output why I'll not be getting my output which is important object or reference in order to call a method in order to call a method what we need object or reference we need an object exactly exactly right click run as Java application we will be getting an null pointer exception very good guys very nice very nice so you are following that is very nice very good so here if you are writing like this we'll be getting in another pointer raises raises n ull pointer exception it is the same null Point exception okay now for example guys my question is for example I'll do one thing let me remove this from the comment and here right now right now if I am trying to write a o b j dot method one [Music] now tell me am I going to get my output here am I going to get my output here okay hang on I'll do one thing wait wait wait wait wait wait wait wait wait ah now tell me now tell me am I going to get my output here here guys please observe line number 24 is not in comment line number 24 is not in command guys 2.
both are referring to this object is we are assigning this reference to another reference where is a Omega two pointing your visual 2 is pointing towards this object so we should be getting our output please take this we'll be getting our output first this object will be destroyed I hope you're understanding me guys right yes yes yes yes yes yes very good very good guys very good okay let me keep this in comment now now okay I'll remove this one also you'll be getting confused okay now this is first one this is second case second case now third case guys what is the third case third case all the objects which were present inside a method okay I'll do one thing guys please observe here inside method one inside method one I am trying to mu Class A like this and I want to give a reference also AO B J belongs to class A like this okay now simply I am trying to call this method one I'm trying to call this method here okay now please observe right click run as Java application and garbage has been collected sir it is a referenced object yes even though it is a referenced object guys every object present inside a method will be destroyed this is one of the super power of java it will be taken care by the garbage collector only then you should be getting a question sir what about the objects which are present inside the main method guys the objects which were present inside the main method also will be destroyed whenever you are shutting down your jvm shutting down your jvm means simply close your Eclipse automatically the objects which are present inside the jvm also will be destroyed this is Third Way Third Way all the objects just let me try to write like this I mean third third that is all the objects present inside the present inside the method also we be destroyed okay guys do me a favor can you please have this program for me please please have this program please foreign foreign informative convergence happens why we are using protected void finalize why because alpesh kindly please go through with object class methods nodes you will understand that go through with object class methods notes and Method overriding concept then you will be getting a clear-cut picture buddy okay uh who is this nickel nickel asking me sir we'll finalize methyl executed only if garbage collector is called yes you want to write a obj equal to 20 in line number 20.
do that and tell me upendra write that write that and tell me write that and tell me whether you can do it or not okay those are methods Krishna those are methods just like methods okay uh sushil no both the references both the references are present uh social references both will be present but but the object which is referred to this particular reference will be destroyed okay fine right right right right all right guys I hope everyone have written the program is it done guys is it done so chill yes yes we are assigning this reference to this reference am I right or wrong so this will be there exactly exactly right right right right all right guys all right guys I hope it's clear for uh evidence and we'll be trying to continue our sessions again bye Monday guys Monday okay so guys I already told you tomorrow you don't have any session take a break take a break and by Monday we'll be trying to continue our sessions guys right and uh upendra buddy upendra you are asking what is that you're asking upendra tell me okay that method is not available anywhere in Java upendra whatever the method which you have written that is not available anywhere in Java where you have it where you have seen that method where you have seen that method do you see anywhere did you see that anywhere yes that's what I am trying to say here whatever the concepts which we have discussing which we are discussing in that particular Concepts you will not be getting those kind of methods right right right right so guys on Monday we are going to start discussing about clone method okay on Monday we are going to start discussing about clone method and after that we will be going with IO strings and shrikanth TR saying sir nowadays class is going only by one hour okay uh guys actually I whenever I was starting like when I'm starting my class I'll not see the time like when I'm ending my class I think I already told you this I'll be continuing the class basing upon the topic basing upon the topic okay we'll do one thing guys we'll do one thing um okay right now I'll be trying to explain about clone method also is it okay for everyone guys is it okay for everyone I'll be going with clone method also right now is it fine foreign ly like with you guys the thing is the thing is guys after 9 30 like after 9 30 if I'm trying to continue the class I am getting the messages from the students I don't want to mention the names so like data is getting completed already 9 30 I'm getting messages like that and one more thing is basing upon the topic I'll go with the time okay so you already know that like I love code guys literally Java excites me okay so if you want tell me I'll be trying to extend the timings there no problem what is your opinion guys I mean honestly guys you can speak with me you know that you know that okay guys I uh please check this I mean some uh guys are asking me sir how many days will be there how many topics are remaining in core Java guys the thing is the thing is maximum maximum by April and are below 15th 10th or maximum by April 10th guess uh 10 days on an average 10 days 10 to 12 Days guys 10 to 12 days we'll be trying to complete our entire course Java okay guys and uh do me a favor do me a favor so as some people are requesting me to increase the timing there so guys from Monday onwards I'll be trying to take our sessions below I mean above 9 30 below 10 o'clock from Monday onwards above 9 30 below 10 o'clock fine guys so everyone kindly please check your mobile data connectivities also I'm seeing you right now only fine above 9 30 below 10 that's it right guys right right right uh please arrange your Rajesh to see you need to arrange that please I understand I understand but I know like you guys are some people are already working but let us what happens like fine all right guys all right all right so thank you guys thank you for listening and once again I am seeing you I wish you you and your family members a very happiest very very happiest for you and your family members and I know that next year I know that next year this Festival you are going to celebrate with your colleagues and whenever you are celebrating with your colleagues remember my words okay last year Christian said like that okay guys all right guys thank you guys thank you for listening yeah thank you for your love all right guys thank you
Related Videos
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 viewsβ’2026-05-28
How agent o11y differs from traditional o11y β Phil Hetzel, Braintrust
aiDotEngineer
450 viewsβ’2026-05-28
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanationπ―β
LearnwithSahera
1K viewsβ’2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 viewsβ’2026-05-29
Search Algorithms Explained in 60 Seconds! π€π¨
samarthtuliofficial
218 viewsβ’2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 viewsβ’2026-05-30
Introduction to Problem Solving Part - 1 | Lecture 1 | Intermediate DSA
ascensionix
107 viewsβ’2026-05-29
So What's Odin Lang Even Good For
TechOverTea
131 viewsβ’2026-06-01











