hello all welcome to this session in this session I'm going to answer one of the Java interview questions that is what is the difference between final and finally keywords in Java let me answer this question with practical demonstration final and finally both are keywords in Java guys predefined keywords in Java there are many keywords that is many predefined keywords are there in Java out of all the keywords final is one keyword and finally is another keyword in the Java programming so the name resembles the same you see final finally they look same but their usage is completely different guys okay though they though they keyword names look almost similar but their usage is completely different guys so coming to the final keyword in Java programming okay this final keyword can be specified before variables methods and classes and on specifying the final non access modifiers keyword before this variables methods and classes their behavior will get modified I'll show you practically what will happen if you specify the final non- access modifier keyword before a variables before variables the the variables will get converted into constants guys okay the value of the variables value of the variables once initialized cannot be updated value of the variables final variables cannot be updated it's a simple thing okay when you specify the final non- access modifier key uh modifier keyword before this variables the value that is initialized to this particular variable in Java programming cannot be updated further okay only one time you can initialize after that you cannot modify the value of the variable okay nothing but the final variables will become constants but what will happen if you specify the final modifier before the methods these methods these methods cannot be overridden cannot be overridden by the child classes you cannot override this methods in the child classes that will happen with the methods if you specify the final modifier before the methods or final keyword before the methods that particular methods cannot be overridden and the third thing here is when you specify the final modifier before the classes okay then that classes cannot be inherited cannot be inherited by other classes no class can become the child class of the final classes simple or final class cannot become the parent of any other classes in Java anything okay and coming to the finally keyword coming to the finally keyword it's generally Us in the exception handling whether the try catch block will get executed or whether the catch block will get executed irrespective of that okay irrespective of whether the cat block gets executed or not whether the exception will come if the exception comes the catch block will get executed if the exception doesn't come only the try catch block TR Tri block will get executed irresp of whether the try block will get executed or cash block gets executed finally block will compulsory gets executed no matter what the finally block will be executed guys okay there we use finally generally this finally keyword in Java is used to close the objects guys okay due to some XYZ reasons you got an exception and because of which you have not closed one of the object of a particular class which is written in the tri block because in one of the line before that you got an exception immediately the control will go to the cat block so whether the exception comes in the tri block or whether the exception doesn't come in the tri block mandatorily finally block will be executed finally block will be compulsory executed okay so that you can write the code for closing such kind of objects in the catch block finally block sorry okay let me practically demonstrate so that you'll get even more better understanding of this final and finally keywords and how to use them in Java and you'll be able to confidently answer this question when it occurs in the interview for that I'll switch to Eclipse ID guys and once I switch to Eclipse ID guys once I switch to Eclipse ID here I'm going to create a new package I'm just going to create a new package is I'll just name this package as uh uh modifiers already modifiers pack is there here I can go to the modifiers pack and here I'm going to create another package here I'll just name this package as do pack a in the inside the package I'm going to create a class inside this package I'm going to create a class right click new class here and I'll just name this class as uh sample a class uh click on finish and inside the sample a class guys I'm going to create a variable okay inside the sample a class that is going to get created here by Eclipse ID okay the sample a class is created inside this class I'm going to create a variable guys okay string name I say okay string name I say so here you see I'm not getting any error right I'm not getting any error so what I can do here is I can create a method here public wide sample method sample method inside the sample class I'm creating a sample method guys inside the sample method if I give the if I update the value to this I initialize the value of this particular name right normally this will happen and if I want to update that to warun let's say name is equal to the latest updated value will be stored into the name right fine this is what I generally do right we generally do like this okay string name and Method will be there all good but what if I specify final modifier here you will immediately get an error here okay you the final field cannot be assigned cannot be modified you cannot modify the value of the final field even I comment this lines also you're getting this error because the final non-ais modifier specified variable should be initialized in the same line okay this particular variable should should be initialized should be initialized in the same line okay we cannot initialize in the method Etc you see earlier we didn't get any error but this time you're getting error and here I'll give M and here if I try to update the value of this final specified variable right it cannot be updatable okay you have to iniz in the same line where you have created and also you cannot update it because it's a constant okay warun davat like that I would like to give but it will not happen guys you'll get an error saying that the final field cannot be assigned okay this is not possible is not possible guys okay final Fields cannot be updated now here I'll write a print statement here system. out. prln and here I'll write inside inside sample method some code I'm writing uh so what I will do is I'll create a child class guys okay sample B I'll say sample B extends extends sample a I'll mention okay sample B extend sample a I'll mention guys and I'm going to copy paste this I'm going to copy paste this guys okay I'm going to copy paste this particular method I'm going to copy paste this particular method in the child class uh the same method which if the same method in the parent class is exactly mentioned in the child class this particular method is said to overwrite the method in the parent class even you write theate override notation if you're not getting ER any error that confirms that this particular method is an overridden method of the parent class method okay so in this case guys in this case if I you see before this particular method I didn't give any final modifier but what if I specify the final modifier the moment I provide the final modifier you will get the error here guys because you cannot override the final modifier specified errors from their parent classes in the child class okay the final modifier specified methods cannot be overridden that's what will happen guys if you try to override the okay methods from the parent class in the child class and if that particular method in the parent class is a final modifi specified method then you cannot override it I just comment this out and apart from that apart from that let's comment this out apart from that you see there is a class and this particular class sample a is a parent class of the sample B and there are no errors but the moment I provide final modifier here immediately you're going to get an error with this the type sample B cannot subass the final class sample a sample a kind of classes cannot be inherited that means you cannot create child classes for the final classes you you cannot modify the value of the final variables you cannot override the methods which are specified with the final modifi in the child classes okay overriding is not possible for the final modifier specii methods so this is all about the final modifier guys this is all about the final modifier then what about finally let me take one more example here under this package I'll just create something like you know uh finally right Class A some sample class I'm creating with main method I'm creating inside this main method I'm going to create some code guys inside this particular method I'm going to create some I'm going to write some code guys what is that code I'm going to write that is I'm going to write some code like in a is equal to 5 comma B is equal to 0 comma C then I'll say C is equal to a / B then I'll write file input stream FIS is equal to new file input stream like this I have written okay I'm creating an object for the file input stream hold the mouse on this import this now uh yeah for the most here we have to provide the file path here you can provide the file path guys like a new file off just represent some file guys uh just represent some file new file off uh for the M on this file and import this uh okay so here what it is actually expecting let's see that file input stream is expecting the file okay object difference of the file that is nothing but new file of new file should come here circular brackets in that give the double codes for the mouse okay change to okay file input stream f is equal to new file input stream and here go the mouse and uh okay control Shift 4 I'll say sometimes it may not happen the things you're writing correctly but you know that the suggestion is not coming control shift you mention you see file got imported from java. package and here is asking me to surround with TR block I am surrounding this with TR block guys I'm surrounding this with TR block try catch here I'll provide something like this catch uh here first I'll say arithmetic arithmetic exception e what the M on this okay meic exception for the M on this and you know here also there's one more exception file not found exception right again cat block you write here write now write on file not form exception you can write multiple catch blocks guys you can write multiple catch blocks here I'll simply write down system. Artic exception got handled here Artic exception got handled here okay here I'll write down file not pH I'll write down import this file not pH and write down file not F exception got handled here file not F exception got handled here okay like this I'll write the code guys okay here I'll give the path of a particular file do we have any file here or if this particular project doesn't have any files we can take it from outside or something okay so let's go to One location in my machine and then let me give the path of any particular file a particular text file I generally prefer so let's see where we have that badge files and here ar. txt file is there right click on this select show more options and select properties guys and here you'll get the full path full path you'll get copy this path come back here and provide the path of the file and similarly double forward slash double backward slash and give the ar. which is the name of the name and extension of the file all good but you can see a warning message here to close this the the value of the local variable field is not used or something is coming here anyhow I'll just take it outside and here put a semicolon here okay here remove the duplicate remove this declaration double declaration you remove once that will be okay here what kind of thing is coming is not used is coming let's try to use it guys let's try to use it how to use it FIS dot read f. read method or something I'll calling that I'm not going to use that but I'm just you know here one more exception IO exception is coming catch catch IO exception exception e col uh like this system. Ln system. out. Ln I exception got handled here got handled here over the mouse on this and import this IO section from java library after this is done nothing is there right uh file input stream uh what is that buffer reader BR is equal to new buffer reader for the mouse on this import this I'll write some code guys I'm trying to explain uh this code is not important for you but I'm trying to explain something that I'm not able to get it okay hold the mouse on this okay and here I need to pass okay otherwise file what I can do here is after this I'll just remove this lines guys okay I'll just make it simple otherwise okay why to make it complex so I need to close one object guys generally what is the type of the object I generally need to close once the task is done what is you see I need to close some objects sometimes okay I need to I thought like file input stream is something kind of that but here it is not coming uh then what maybe the object that I need to close Okay uh I remember that that is for reading the data from the output conso okay that is uh what is the class we have to use for reading the data from the output console from the user that is nothing but the scanner guys scanner scanner semicolon control shift scanner is equal to new scanner of here system. in I'll write down put a semicolon here this will read the data from the user why if I put something in and scanner dot next change scanner do next change like this I'll write something now this particular thing guys this particular thing okay this scanner object I need to close guys once the task is done right generally we need to close Okay the scanner object need to be closed but if I try to close a scanner object here scanner do close like that okay I'm trying to take a simple example why to make the things complex there the reason I have removed the file input with file input stream and also I can explain but I thought to make the example simple so I have used scanner object the scanner object need to be closed guys okay if you putting the scanner directly inside the method right you will be able to see that okay for example here if I write something like this inside the method directly right okay if the scanner object is not closed the scanner object is not closed you'll get a warning message asking you to the local variable is not used okay let's use it scanner dot scanner dot next in like that I'll write some scanner. next or whatever it is you see still the warning message is coming scanner is never closed is coming okay so if you write that you're getting the warning message outside but when you put that particular thing inside right inside the tri block you're not getting that but but outside is getting but whether inside it is not coming or not doesn't matter guys inside whether inside the tri Block it's coming or not doesn't matter we have to still close okay here you see scanner is not used is coming we'll try to use it scanner dot next in or something I'll write down here and once it is used right we have to close as a good practice we have to close a scanner object if I close it here and due to some XYZ reasons if you're getting an exception in the 12th line because you're trying to divide a number by zero iide by zero this particular line is going to give you this particular line is going to give you arithmetic exception because of which this exception right this lines will be skipped and will directly go to the cat block will scanner will close object here the object is created already for example let's see let's write it here the object is created let's say okay you see I'm writing like this scanner scanner is equal to scanner isal new scanner I'm writing like this here scanner is never closed it is saying I need to close it right I need to close it okay but what's happening the object is created next line before it closes the scanner object guys next line when I say C is equal Aid 5 ided 0 will result in an exception because of which the control will go to the catch block and because the control goes to the catch block right uh you know cat the statements inside the catch block will be executed and the system. end of this program will happen end of this program will happen Okay arithmetic exception got handled here will happen and end of this program will be printed will the object will be closed this scanner object will be closed no the program will work fine this is not an error there is a warning message athematic exception got handled here end of this program came then finally here if I write finally block here no matter whether this particular exception comes at this particular line or not okay uh if an exception comes also I want to go to the final block if the exception doesn't come also I want to close the scanner object in the finally block here I simply write scanner. close guys okay now see guys here some in variable is coming just initialize that with null for a while okay that will be okay so here whether the exception comes at this particular line whether you go to the cat block doesn't matter if you put the same scanner do close here and the exception comes here in the tri block the remaining statements in the tri block will not be executed and because of with the scanner class will scanner object will not be closed but with this solution right by providing the finally block we can close a scanner object scanner object if it is created and after creating if exception comes right any reasons if an exception comes and if this scanner do close is not reable then the finally block will be executed and this will get closed okay simple no matter what the system. out. println scanner object is closed scanner object is closed whether the whether the exception occurs or not this time exception will occur because you are trying to divide a number by zero after the scanner object got created but here scanner object got closed guys but if I change this value of V to one in this case exception will will not come 5 divided by one exception will not come and it will intake the input from the user uh or you know whatever it is uh let's not intake the input from the user guys right cck run as jav obligation right cck run as ja obligation and see that still the scanner object is closed whether the exception is coming or not doesn't matter guys the scanner object will be closed the scanner object will be closed of whether the exception comes or not the scanner object will be closed Okay C is equal to a b sometimes it's giving an exception when B is zero sometimes when the B is one it's not giving the exception whether you are going to the tri block whether you are missing the cat block doesn't matter guys okay whether some only some lines of the tri block got executed at the end of the tri block ultimately finally block will be executed and the object this kind of objects will be closed and this is the main purpose of using the finally blog in Java programs guys to close this kind of objects which will not be closed due to some exceptions occurring during the trach flocks fine guys so hope guys with theoretical explan and practical demonstration have explained explain the differences between the final and finally keywords okay in Java programming okay they their naming is similar but their functionality is completely different guys their usage is completely different so that's all for this session guys thank you bye-bye hello all welcome to this session in this session I'm going to answer one of the Java interview questions that is can you access the private methods from outside the class the straightforward answer for this question is no directly if you try to access the private members of a class like it may be variable or method you cannot access them guys whether you create an object or you make them whatever it is okay private variables and methods cannot be accessed outside the class only within the class you can access the private variables and methods but there are some other alternative ways still if you have to reach out to that variables and methods which are specified as private okay using encapsulation concept there is a possibility but the straightforward answer for this question is no guys let me explain all this stuff in a detail manner for you in this session so for that I'll switch to this Eclipse ID and here I'll create a new package I'll create a new package guys that is uh right click new package I'll just say py under this package I'll create a class I'll just name this class as Class A and under this class I'll create some private variables and uh private methods let's say okay like in a is equal to 5 like some method is there public wide sample a method is there inside system when when you call this particular method right inside sample a I didn't mention any private modifier or private keyboard keyboard before for these variables and methods of this class what if I create one more class what if I create one more class like demo class with main method I'll create and if I want to access this variables sign methods okay if I want to in this demo class from the demo class which is give me a second let me drag this so that you can see the previous class here let's assume that before this variable public modifier is specified and before this method also public modifier is specified in such kind of even before the class also public modifier is specified in such kind of cases if I want to access this variables and methods which are of public type outside this particular class in a different class if you want to access can you access yes by creating object for the class class CA is equal to new class A okay Class A is equal to new class A I'll write down and simply write down uh system. out.rn system. out.rn CA do ca. a ca. a I say and here CA do sample a I'm able to call this particular public method directly outside the class I'm able to access the method by using the object reference by Crea an object for the class I can do that and if you run this code you'll get a proper output also because this variables and methods are specified as public it's possible you can get the you can access the variable private public variable outside the class public method outside the class it's possible but if I make them private if I if I remove this public and in place of that if I provide private okay before the variables and methods if I do that you see already I'm getting an error the field Class A is not visible you cannot access this guys you cannot access the sample a method from outside the class is not visible errors are already coming in the two statements because the private members of the class cannot be accessed outside the class within the class you can access guys for example if I create one method here like within the class public wi sample B if I create and within the class if if I try to access this system. out. prln I can directly access see system. out. println a I can access I can also call the method sample a method I can call and access you see no errors are coming within the class but outside the class private members of the class cannot be accessed but there is a way guys but there is a way directly the answer is no and the interviewers are also expecting the answer for this particular question as no but there is one alternative way guys that is what if you create what if you create a public method here what if you create some public method here okay public wi sample B kind of method if you are creating and from this sample B you're calling sample a indirectly okay but you cannot access the sample a method directly here you cannot access the sample a method you see when you say ca do sample a okay you already getting error directly the private members of the class cannot be accessed directly outside the class you'll get this kind of error message because they can be access only within the class so as part of encapsulation what I will do is I'll create a public method okay in the class where private members are there I'll create a public method in that public method I'll try to access this private members of the class okay now instead of calling the sample a what if I call sample B what will happen guys I can call sample B guys because sample B is a sample B is a public method so by create an object for this class outside the class I can access this method and by using this public method I can call the private method so indirectly when I'm calling sample B sample B is calling sample a directly I cannot access sample a outside the class but here I'm able to call the sample a with the help of the sample B okay with the help of sample B which is a public type okay these called as encapsulation guys in encapsulation directly you cannot access the private members of the class outside the class whereas by using the public methods you can access them outside the class with the help of public methods in the class where the private members are there you can access them outside the class inside sample got printed still okay so this is what I'm trying to explain here guys okay can you access the private method or methods from outside the class the answer is no directly it is no but with the help of encapsulation concept we can trick it and get the uh we can access that indirectly but directly it's not possible guys actual answer is no for this particular question but if you explain that encapsulation concept when you get this kind of simple question right the interor will definitely get impressed because you're not only answering the question question you are also giving a solution of how to access the private members outside the class with the help of public methods okay which is nothing but encapsulation object oriented Concept in Java so with practical demonstration and theoretical explanation I've answered this question for you so that's all for this session thank you bye-bye hello all welcome to this session in this session I'm going to answer one of the Java interview questions that is how to convert array to array list and also how to convert array list back to array in Java let me answer this question with practical demonstration okay there are two things that I need to answer as part of this question one part of the answer is how to convert an array to array list in Java and another part of the question or another question as part of this question is how to convert array list to an array okay these two things I need to answer as part of this question so so there is nothing guys that I have to explain here directly I'll go to the Practical part and show you how to convert an array to array list and also how to convert an array list to array okay so for that first I'll create a new package here just for separation purpose I'll create a package under this I'll create a new class I just name this as converting arrays to array list how to convert arrays to array list in Java okay demo with main method I'll create first first I'm going to create an array guys first I'm going to create an array that particular array I'm going to convert into the array list okay for example this is an array integer array AR is equal to okay or ar1 is equal to okay here I'll write down 9 comma 1A 5A 7 comma 6 comma 2 comma 8A 4 some elements random elements I give okay fine integer array ar1 is equal to like this I gave now this is an integer array this is an integer array which I need to convert into the array list first in order to convert this array into the array list first I have to convert this particular array into the list okay for that we have to write ar1 okay for that we have to write there is a command guys arrays dot as list command okay there's a predefined class known as array and we have a predefined method static pred methods of the AR class known as aist we have to write and to this asist we have to con copy this ar1 okay we have to copy the object difference of this integer array okay then this as list will return return you the list type okay list is equal to for the M on this and say create the return return type of this as list will come here that is list of integer is coming I was able to convert I'm able to convert the integer array into a list but my question here is how to convert an array into an array list that's what is the question but here it got converted to the list if you still want to convert this into an array list you have to write like this array list a list is equal to new array list new array list okay give the circular brackets here give the circular brackets here and in this circular brackets you pass this list guys you just pass this list for the mouse on this import this from java Library and here you have to provide the same generic type that's it guys by passing this list into this Constructor of the array list you can convert the list into the array list so there are two this is two step process guys in order to convert an integer array or a kind of array into an array list first we have to convert the array into the list using this arrays. as list method you have to convert this and you have to pass this particular object reference of the array and then the array will be converted to the list and that list you have to pass to the Constructor of the array list class so that it will be finally converted to the array list now if I print this out system. out. prln aist if you don't get any error this confirms that an array has been converted to the array list in Java okay right click run as Java application you see the array list got all the elements in the array list got printed okay whatever the elements added to the array are now part of the array list okay so this is the code guys guys if you want to practice this code you know you can take a screenshot of this code and practice in your local maion or person machine okay so hope guys you have taken the screenshot of this code now let me explain another one that is how to convert array list to an array reverse one array list to array demo array list to array demo with the main method I'm going to create first I'm going to create an array okay first I'm going to create an array list guys first I'm going to create an array list array list a list is equal to new array list okay circular brackets put a semicolon here array list a list is equal new array list and here you provide uh let's say integer type and I'm first creating an array list which I need to convert into the arrays let me add the elements into this array list a list do add of let's say n okay I just add only few numbers guys few elements only 9 comma 5 comma let's say a list. add of you know 7 and then aist do add of one like this I'll add some four elements guys and here I'm going to I want to convert this is an array list guys I want to convert that into I want to convert that into an array okay array list I need to convert it into an array for that to happen guys first you need to create an array integer array integer array ar1 is equal to new integer array and how to give the size of this array guys how to get the size of the array the size of the array should be equal to the size of the array list so I'll just say a list. size here this will dynamically get the size which is four okay four will be added here and once I create this array guys then I have to say then I have to say there is a a list dot using the object reference of the array list you have to call a method that is two array method okay you have to call two array method and using this two array method I can convert the okay two array method and in this two array method you have to pass the object reference of this integer array and here I have to store into the same object reference of the integer array by doing this we can convert an array list to an integer or to an array system do how to find out whether it has been converted yes or no here a one of zero if you write print right the first element that is nine will be printed system. out. prln AR of AR one of one you just give then system. out. prln one of two you can print then system. out. println system. out tell AR one of three like this if I write and run this code if I'm able to get the output means successfully the array list has been converted to the array guys you see all the elements of the array got printed after converting from array list to array so these are the lines we have tried guys here lot of people forget to pass this object reference of this array okay so here instead of just using this object reference to assign the resultant here in the two array method you have to pass the object reference of the the Destin array okay that you are going to converted to okay so here lot of people do this mistake where they don't pass this and they get the errors they will not be able to convert okay so hope guys you got the answer for this question and uh similarly guys you can take the screenshot of this code of how to convert the ARR list to the array okay you take the screenshot of this particular code and practice this in your local machine so that you'll get confident about this programs okay so hope guys uh you got the Practical demonstration uh let me let me do one thing with practical demonstration I explained this uh explained the answer for this question that is how to convert the converting array to array list what is the code guys what is the code uh converting array to array list arrays to array to array list okay this is a code guys for converting the array to array list this is a code for converting the array to array list I just copy this code guys and then paste it here okay so that you can get in the notes similarly how to convert converting AR list to array how to convert the array list to array also have written the code array list to array and this is the code guys I'll copy this code here and put that in the Cod section so that you can get it from the notes okay I think hopefully have successfully copied the code into this uh you know converting array to array list and converting array list to array okay so with practical demonstration I have answered this question for you so that's all for this session guys thank you bye-bye hello all welcome to this session in this session I'm going to answer one of the Java interview questions that is what is the actual purpose of specifying static keyword before variables and methods in Java let me answer this question with practical demonstration in Java programming when we specify the static keyword before variables and methods these particular variables and methods can be accessed outside the class without the need of creating an object if the static keyword is specified before this variables and methods then you if you have to access the static variables and methods outside the class there is no need of creating an object guys there is a main purpose or actual purpose of why people use static keyword in real programming the conceptual wise theoretical wise there may be many other reasons of you know static keyword specifying before variables and methods But ultimately when coming to the realtime purpose most of the people will use static keyword to access this variables and methods outside the class without the need for creating an object that is without creating an object for the class in which these variables and methods are available we can directly access with help of the class name okay class name do variable class name do method okay if they are static if they are not static then you have to create an object for the class inside which these variables and methods are available and using the object reference of the class you have to access this variables and methods there we need to create an object if the static keyword is not specified but if the static keyword is specified there's no need to create the object directly using the class name we can access the variables and methods outside the class okay so let me practically demonstrate this for you guys so that it will the answer will become more easy and better to understand for you for that I'll switch to this Eclipse ID guys in this I'll just expand this and go to one of the package here let me go to this package and here I'll just give a name some other package for just for organizing and categorizing I'll create this pack e I'll say Okay Factor name py I'll say that's it and under this package I'm going to create a class I'll just name this class as car class let's assume car class click on finish and inside this car class I'm going to create some variables and methods let's say string okay string color of the car similarly I will you know create something like you know some method public wide start car method I'll simply write down system. out. prln system. prln system. out.rn car started car started like this I'll create a class and here class class I'll create one more class guys I'll just create try to create one more class or I can create separate class otherwise so I create in the same file I will create a separate class create a new class I'll just name this class as let's say demo with main method I'll create inside this inside this guys inside this I'm going to okay I'm going to create an object for the car class car car is equal to new car car car is equal new car like this and using this object reference okay since this particular variables variable and method that are created inside this class in this particular variable and method that are created inside the class are not static type okay I need to create an object for the class and say car dot using the object reference I to say car do color is equal to let's say flat color okay then uh car dot start car method I need to call using by we have to create an object guys otherwise we cannot do you have to create an object for the class in which this non-static variables and methods are available and using object of that particular class we have to access the variables and methods this is what we have to do this is what we have to do. car dot color I'll print the color of the car also run this code and the code will run and you'll get the output everything will be fine you see black and C started has been printed everything is working fine but what if I make this particular variable as static and this particular method as static by specifying the static keyword before this variable and Method like this in this case this particular line is no more required guys directly you can say name of the class is car right give that here in place of the object reference you have to give the name of the car and you can access the same way name of the class class name dot variable name class name dot class name dot method calling static method can be called directly with the help of the class name here do we need to create an object did I create an object anywhere no if they're static they belong to the class memory they belong to the class guys where if you don't specify static they are like instance members of the class they belong to the specific object of that particular class hence we need to create the object in that case but here in this case this particular static variables and methods belong to the class hence we have to access them using the class name we don't have to create an object because they are not instance variables there are static variables now which belong to the class hence using the class name we can access so this is ultimate purpose why people uh mention static keyword before variables and methods so that they can access the variables and methods outside the class just by using the class instead of creating an object for that still the output will come here the same output will come guys black and you know start C started will come so hope guys with the Practical demonstration and theoretical explanation hope I have answered this question properly for you and hope you have understood the answer for this question in a better way so that's all for this session thank you bye-bye hello all welcome to this session in this session I'm going to answer one of the Java interview questions that is why we have to use the synchronized Block in Java let me answer this question along with practical demonstration so what is the use of the synchronized Block in Java in Java programming we can create a block known as synchronized block by specifying the synchronized block what is advantage or what is the purpose or use I'm going to explain guys so this is specially used the synchronized block is specially used in multi-threaded programming you see in a single Java program when you have multiple threads running parall at the same time there is a possibility that this multiple threads can try to update the value of a single variable at the same time what happens when one thread is already trying to update the value of the variable another thread is going to disrup that process and interrupt that thread and try to change the value of the variable to some other thing like that a conflict will be there for example guys if you take an example of a hotel okay where you know some 100 tables are there and all the people okay who need to you know uh who need to have lunch or dinner or breakfast have stepped into the hotel and have sat sat on the table and that particular hotel management to you know to accommodate all this 100 uh you know 100 chairs and all 100 you know tables and all they have appointed not just one waiter or you know server they have appointed like around 15 to 20 people to serve the 100 tables so there may be some situation where at the same time one server if it is going one server or V is going to take the order from the table same time another server may come to the same table and again repeatedly take the same order okay so there is a possibility there is a possibility that the same table can be served multiple times by taking multiple orders in licate man manner same happens in Java programming in multi-threaded environment where multiple threads okay though they are designed to perform different activities may try to perform on a single thing at the same time because of which interrupted exception kind of exceptions may come okay so but what if you want to make sure that that particular variable for which the threads are trying to update the value should not be updated at the by multiple threads at the same time to the interrupted exception multiple thread should not come at the same time and update the value of the thread well one thread is already updating the value of the variable other thread should wait until the value of that particular variable is updated other thread should wait okay and once the thread has completed the T the other thread will try to update the value of the variable while that particular thread is updating the valuable value of the variable then next should not come and disturb that it should wait so how to make sure that how to protect that particular updation of the the variable so that multiple threads will not do activity at the same time because of which we may get interrupted exception the answer is with the help of synchronized blocks with help of synchronized block we can avoid okay we can put that we can provide that updation of that particular variable code inside a synchronized block okay and by doing that right the value of the variable can only be updated by one thread at a time okay the multi multi thread environment is there only one thread can update other threads will wait okay and uh you know because of it things will go fine for example with example have to with practical demonstration if I explain this right you will get more clarity of this particular answer for this question for that I switch to Eclipse ID guys here under one package like uh I'll create a class and I'll just say name this class as my account my sorry bank account bank account or my account whatever it is Bank you you know the example of the bank right click on finish in Bank what be there in Bank generally why we have to check our bank account to see how much amount is there in our account how much balance is there okay so here enter balance balance amount or balance some variable I'll create I'll assign this balance to zero initially the balance in this particular bank account is zero and I would like to deposit some money okay I would like to deposit some money for that purpose public wi deposit okay deposit method I create using this deposit I would like to deposit some amount how much amount I would like to deposit in okay in some I or something some number some amount okay in amount I'll say otherwise int amount some amount I want to deposit this amount should be provided as a okay when you're calling this particular method you have to pass the amount such amount will be deposited into the bank account how I have to write the code like this balance is equal to balance plus amount by writing this code okay this particular method is going to update the value of the balance from zero to whatever the amount I want to deposit if I deposit you know one rupee here or one Buck here 0 plus one it will become one Buck okay so now if I want to get the current value of this balance okay after depositing some manyy or before depositing the money if you want to get the current balance you'll create a method like get balance get balance okay get what is the current balance return this variable value have to return return guys variable value I have to return the variable is of in data type so the return type will be in now in this kind of situation this is a normal program normal program I'm not implementing any synchronized block Etc in this program very basic program just try to understand the basic program guys in the next level I explain about the synchronized block kind I'll make this multi threaded you know okay so like that F fine okay now in this case what I'm going to do here is here I'm going to create a demo class and with main method normal program I'm writing so that you can understand the context first after that I'll go to the multi threading and synchronized programs synchronized blogs Etc okay now now guys if I want to deposit some money what I have to do guys I need to create an object for the bank account okay bank account account is equal to new bank new bank account like this I'll create an object for the this one okay bank account class bank account bank account u n okay I'll copy this K is missing here the name here I have to put K so that it will be gone bank account okay like this I create an object guys if I want to deposit some money what I have to do I have to say account dot deposit I need to call deposit and if I want to deposit let's say one rupe into this Bank okay after depositing if I want to get the balance so simply I'll write system. out.println account do account. get balance I'll say the balance whatever the amount I deposited right if I deposit only one rupe the balance will be updated to one rupe here and since this particular program is a single threaded program you see main method is only running with the main thread is running it is a single threaded program only one thread is running which is nothing but the main thread if you run this you see you got the balance as one if you try to deposit multiple times okay still it is a single threaded program only guys this time you should get the value as four because four rupees you have deposited so balance should be four at the end of the program hope you have understood this program now but now I'm going to convert this into multi- threaded program okay how how to convert this into a multi thread instead of calling this particular thing as part of you know code what I will do here is anyhow at the end of the thing I'm going to get the balance I'll keep it like this but in between what I'm going to do here is I'm going to create some threads guys okay I'm going to create some threads thread T1 is equal to new thread T1 is a one thread okay like this I'll write and I'll create one more thread thread T2 is equal to new thread and in the Constructor of this in the Constructor when I create this particular thread right okay when I create this particular thread right in the Constructor of the thread I'm going to create a function guys and easiest way of creating a function is by using nameless function which we call as Lambda expressions in Java so there doesn't have any name so circular brackets will be there before the circular bracket this method doesn't have any nameless function or nameless method doesn't have any name guys if it has zero parameters then circular brackets are mandatory here zero parameters are there in this function and you have write this arrow symbol and write the starting and ending ending portion of the function or method this starting of the function ending of the function okay so inside the thre Constructor I'm writing guys inside this I'm going to write some code guys the code is nothing but the code is nothing but uh I would like to write some code like this for INT I equal to 0 I equal to 1 sorry I equal to 1 I less than or equal to th000 semicolon i++ I'll write after writing this in this iteration I'm going to call this deposit method guys okay so how to call that method account dot deposit every time I call this method I would like to deposit a single rupee single Buck okay here single Buck how many how many Bucks will be added to this uh balance by this particular thread thousand times this is iterating means every time it is calling deposit one means one rupe one rupe or one Buck is getting deposited and the balance is getting increased by one and if I run this particular Loop this uh th times th000 bucks will be added to the balance right basically but here unfortunately there are no single threads here there are multiple thread this is one thread another thread is also there in this thread also I'm going to write the same as it is logic this thre is also going to do the same thing guys this thre two is also going to do the same thing where again it's going to deposit the rupe one okay like the same thing it's is going to do that here you can actually make it like this it's more better okay this is better way of writing fine there are two two threads this also will add th000 bucks this also will add th000 bucks right th000 rupees or th000 bucks or whatever the currency you want okay now I want to start this threads I want to start these threads T1 do start I have to say then T1 do T2 do start I want to start these two threads so I'm starting the two two threads two threads to deposit the money will keep running simultaneously okay this thread will be repeatedly running this many times you see this is trying this particular thread it is running right it's keeping on repeating and adding balance increasing the balance by one one one one like that and second thre also at same time is trying to increase the balance of this particular same variable both the threads are working on okay the balance variable is getting updated by this both the threads at the same time and every third is trying to add th bucks into that in a by calling this particular method thousand times by calling the deposit method thousand times but is this particular balance variable is now thread safe or is you know thre means multiple threads will come and you know can interact with at the same time right yes it is possible so there's a possibility that interrupted exception may come because of this kind of case okay let's see what will happen guys interrupted exception may come or you know wrong balance may come or something will happen okay let's see that without that uh synchronized block what is happening here th000 bucks here th000 bucks okay here exception may not come here 1,00,000 means total 2,000 balance should come at the end but I will not get 2,000 balance guys sometimes I may get but most of the times I may not get if I run this code guys uh before running guys after starting this two threads there is something like you have to ask the main main thread you see apart from this two threads the actual program has a main thread right I want to uh make the main thread weight and print this balance for that reason to make the main thread weight I'll say join I'll just join this particular threads to the main thread so that main thread will not be completed until until both the threads got executed main thread will not be completed here you're getting some kind of interrupted exception possible I'll ignore the exception for a while that's okay now this this join method will make the main thread to wait until the threads are completed their work and after these two threads are completed with their work then main thread will be executed and remaining program will be completed otherwise before these threads are completed balance will be printed before this particular threads are completed the balance will be printed so we have to make the main thread wait until the this particular two threads are completely added the balance okay that's the reason we are joining this particular T1 T2 thread to the main thread so that main thread will wait until this particular two threads are complete finally this two threads are complete remaining portion of the main thread will be completed okay run this code and see that will you get 2,000 or something let's see that you didn't get 2,000 1 8 34 what happened to the remaining balance where it go on because of the at same time the multiple threads are trying to update the value right the maybe both the threads try to update the value by one itself and because of it some some bucks are lost here let me run it again every time you not get the same value guys every time you may get a different value here luckily we got 2,000 but it's not always guys third time I'm running you see you got 2,000 again we are lucky again I'm running not every time we'll get guys not every time we'll get uh just keep running guys just keep running okay most of the times we are getting now just click on this and see okay almost every time you see 1983 came 2000 came not always 2000 is coming but sometimes you know some some different number is also coming depending on you know how the threat are being you know most of the time 2000 is coming but you know in between sometimes uh you know in between Sometimes some different numbers are coming that means there is no guarantee that the balance amount will be 2,000 because here 1,00,000 tot what if I put this particular balance inside something known as synchronized synchronized block and here I need to write this keyword and provide this like this synchronized okay like this we have to write guys Synchro n z okay synchronized keyword we have to write and this what the purpose of this particular block is very straightforward guys okay inside this roniz block multiple threads cannot interact with this variable at the same time synchronize even though this is a multi-threaded programming synchronized block will make sure that at a time only one thread can access the balance and remaining threats will keep waiting to update the balance so once you have the synchronized block now any number of times you write the code guys any number of times you run the code there is no possibility I'll try to run this third time I have WR fourth time I'm running fifth time I'm running sixth time I'm running seventh time I'm running eighth time I'm running ninth time 10th time not even a single time you're getting a different thing every time you're getting 2,000 there's no possible sometimes in without synchronized block right sometimes we got 1954 1984 like that but in case of when you make this particular variable part of this updation of the variable part of the synchronized block like this like this you write and part make it part of the synchronized block you know only one thread at a time can deposit the balance and update the value of the balance and this is what is the purpose of the synchronized blog guys so with practical demonstration and theoretical explanation I answer this question for you so if you want to practice the same program in your personal machine or personal laptop you can take a screenshot of this code okay so hope guys you have taken the screenshot of this code and hope hoping that you will practice this particular code in your local machine or personal machine or personal laptop so with this note guys I'm just winding this session up uh so I have explained you theoretically practically and was able to answer this question for you if you can answer question like this right with this kind of examples and all when you can kind of get this question in the interviews then it the interviewer will get definitely get impressed so guys and also I'm putting all this code inside this notes also I expand this whatever the code I demonstrate is also part of the notes you can also check the notes guys fine so that's all for this session thank you bye-bye