Transcript for:
Understanding Local Variables in Programming

So, in the last session, just we started about types of variables, correct? In that static variables we covered, instance variables we discussed. Now the last part what we have to discuss, local variables. Keep submitting. Local variables. Local, local variables. Local variables, variables. Okay. Here just observe, sometimes based on programmer requirement, sometimes based on programmer requirement, we have to declare variables inside a method correct or not inside inside a method the variables which are declared inside a method are inside a block or inside a block okay the variable which are declared inside a method or inside a block are inside constructor or inside constructor okay the variables which are declared inside a method or inside a block or inside a constructor to meet the temporary requirements of the programmer so such type of variables are by default considered as what local variables right are you able to understand okay this is sir what is local variable the variable which is declared inside a method or inside a block or inside a constructor to meet the temporary requirements of the programmer Such type of variables are called what? Local variables. Now do you know where we can access local variables? Only within the method where we declared or within the block where we declared or within the constructor where we declared. Are you getting? Within the method or within the block or within the constructor where we declare. Can I use the term? These are temporary variables or not? Yes, because within the method only available or within the block only available, not permanent data. That's why it is also known as temporary variables. Temporary variable. Local variables or temporary variables are the local variables will be stored inside the stack. Stack memory. Stack memory. That's why local variables also known as stack variables. Right. Are you getting stack? Because they will be stored in which memory area? Stack. Stack area. That's why local variables also known as stack variables. Right. Next. Suppose if I consider, sir, for int i is equal to 0, i is less than 10, i plus plus like, okay, SOP of i, like I am taking. Can I use the term, is this i, is it a local variable or not? Local, local variable, local to for loop. I am getting it is a local, local to for loop, okay, this is a local variable, right? Now, Within the for loop available if you come out of the for loop automatically not available within the for loop available If you come out of the for loop automatically not available, that's why local variables, okay are autom variables sometimes available sometimes not available within curly brace available if you come out of curly brace automatically not available that's why local variables also known as automatic variables are getting there are multiple other words are there for local variable local variables also known as a temporary variable or stack variables or automatic variables right I hope you people can ever clearly read it okay add That first point, sometimes, sometimes, sometimes, to meet, sometimes to meet temporary requirements of the programmer, sometimes to meet temporary requirements of the programmer, to meet temporary requirements of the programmer, temporary requirements of the programmer, of the programmer. We can declare variables. We can declare variables. We can declare variables. Variables. Sometimes to meet temporary requirements of the programmer, we can declare variables. Inside a method. Inside a method. Inside a method. Inside a method. Or block. Inside a method or block. Or constructor. Inside a method or block or constructor. inside a method or block or constructor constructor such type of variables are called such type of variables are called such type of variables are called variables variables are called such type of variables are called are called local variables such type of variables are called local local variables or temporary variables local variables are temporary variables stack variables local variables are temporary variables are stack variables are automatic variables are automatic variables automatic automatic variables local variables are temporary variables our stack variables are automatic variables automatic automatic variables automatic variables variables automatic variables variables okay that's all i hope you people can aware what is local variable or what is a temporary variable i mean what the local variable where we can declare second point just now we covered local variables will be stored in which memory area stack memory stack member that's why local variables also known as what stack variables right add local variables will be stored local variables will be stored stored local variables will be will be stored inside the stack memory local variables will be stored inside the stack stack memory inside the stack memory stack memory Memory. Okay, that's all. Are you getting? So this is. Now the third point, I mean this point is the very very important, right? Make sure. Sir, when the local variable will be created, the local variable will be created while executing the block in which we declare. Sir, while executing this method or while executing this static block. or while executing constructor or while executing this for loop so the block in which we declare while executing that block local variable will be created and when the local variable will be destroyed once this block execution completes sir m1 method completes automatically local variable will be gone or static block completion local variable will be gone so so while executing the block in which we declare local variable so at that time local variable will be created once the block execution completes automatically local variable will be gone simple suppose if i call m1 and i legend legend this one if i call m1 local variable will be created and i will use in the next lines of the code once m1 method completes automatically local variable by default will be gone that's all suppose next time i call m1 method once again m1 method next time once it again once again a new local variable will be created after completing this that local variable will be gone suppose if i call m1 method 10 times how many times local variable will be created 10 times 10 times don't feel one time 10 times for every execution of that block local variable will be created after completing that block local variable by default will be gone that's why are you able to understand suppose multiple threads are there t1 t2 t3 like t1 call m1 t2 call m1 t3 call m1 for t1 one local variable for t2 one local variable for t3 one local variable are you getting that's why do you know for every thread a separate call of local variable will be created that's why local variables are also known as a thread safe also also considered a thread safe thread safety is there because for every thread a separate copy by default will be maintained right are you able to understand sir anyway local variable when it will be created when it will be destroyed what is the scope of local variable let me summarize local variable will be created while executing the block in which we declare that once a block execution completes automatically local variable by default will be gone the scope of local variable is exactly same as the scope of the block in which we declared it okay local variables will be created local variables will be will be created local variables will be created will be created local variables will be created while executing the block local variables will be created while executing the block local variables will be created while executing the block in which we declared it while executing the block in which we declared it while executing the block in which we declared it in which in which we declared it in which we we declared it it next up once a block execution completes once a block execution completes once a block execution completes completes automatically automatically automatically local variable will be destroyed automatically local variable will be destroyed local variable will be destroyed local variable will be destroyed destroyed hence hence hence the scope of local variable the scope of local variable the scope of local variable is exactly same as the scope of local variable wait wait wait the scope of local variable is the block is the block block scope of local variable is the block in which we declared it So, I hope you people now can aware what is local variable, in which memory area it will be stored, when it will be created, when it will be destroyed, what is the scope. Now, terminology is very very clear for you people. The next chance or the next point or conclusion make sure. What is the next terminology? Make sure you people should have clear idea about this one. Sir, local variable, instance variable, static variable. Static variable part of class. We are getting class level data. Instance variable. part of object local variable part of method method or part of block for loop part of for loop part of if block like sir local variable okay so local variable instance variable static variable instance under static variables are standard data class level data object level data but local variables are not standard temporary data that's why JVM won't provide much support for the local variable. So for instance variable, static variable, if I'm not performing initialization, what will happen? JVM will provide default value. There is no such type of terminology for local variable. For local variable, compulsory we should perform initialization explicitly. Are you getting? So we should do that. One thing. Next another biggest problem with local variable, scope. So instance variable, wherever object is there, instance variable is there. Wherever class is there, static variable is there. But local variable, within if block it is available, if I will go to else part, it is not available. Or within the for loop it is available, if I will come out of the for loop, it is not available. Scope related issues are dangerous for local variables, right? Take special care. So one possibility to ask the question in the exam related to scope, dangerous concept. Second possibility related to to default values okay first let me talk about scope here see this one very important here i'm taking class test class test public static void main public static void main string array ax class test the public static requirement legend don't take string array ax now i'm taking Here, int i is equal to 0. Int i is equal to 0 for, for, int j is equal to 0. Int j is equal to 0. j is less than 3, j plus plus i, j plus plus. Now, i is equal to i plus j. i is equal to i plus j okay system dot out dot println i dot dot dot j sir can you tell what is the i value and what is the j value what is the i and j i is equal to 0 For integer is equal to 0, j is less than 3, j plus plus. i is equal to i plus j, so p of i dot dot dot j. What is the i value? What is the j value? Yes. What is the i value and what is the j value? yes boss I value is 3 j value is ok make sure if I can do like this first the code won't compile compiler will give left hand right I is local variable of main method within the main anywhere you can access no problem within the main anywhere you can access but j is is local variable of local variable of for loop for loop local variable of for loop within the for loop only you can access outside of the for loop you can't access that's why sir outside you are accessing j what is j or where is the j like immediately compiled a matter saying cannot find the symbol Cannot find symbol which is symbol cannot find symbol which is symbol variable J Variable J in which location Okay, class or tester In which location class or tester like this? I'm going to get are you getting sorry in the test class You are using some variable J where it is available first Let me know then only I can compile because J is what a local variable of for look outside of the We can't access that's why the problem is coming are getting scope related issues a bit dangerous for local variables right next up Very common question for the exam legend legend this one Sir, I'm taking here class or test one more example class test public static void main string array ox Public static void main string array aux. Here I am taking int. Int here 1. Sir, try block I am taking. Try, try block. Int j is equal to integer dot percent. Sir, integer dot percent. To convert string to number. Integer dot percent of t e n I am taking. Ntj.parse into f10 But ten not a number overall word immediately we are going to get what class case exception because string to number conversion but string is not representing a number that's why number format exception catch number format exception haa boss if number format exception will come Number format exception will come if it is the case. Can you please keep j is equal to 10? Sorry, I sent error click 10. Not required to take conversion. And then, can you please print SOP of j? Can you please print SOP of j? If I can take like this, compiler will be left on it. You know what is the reason? Not required to keep explanation. j is local variable of tri-block. J is local variable of tri-block. So outside of the tri-block or within the catch block how you can access? Or outside of the tri-block how you can access? Immersively compartmental by default you are going to get here one time, here also one time. Are you able to get? Related to scope, local variables are a bit dangerous, take a bit special care. I hope you people can aware right, okay? So this is sometimes available, sometimes not available. This question is there in our SCJP practice book also. Compulsory possibility to ask in the exam related to scope of local variables. Take both examples. Take both examples clearly. Cannot find the symbol which is symbol variable j location class test take take clearly completed right Cannot find symbol which is symbol variable J in which location class or test alike So scope of local variables a bit dangerous. That's why compulsory you people should have clear clarity about local variables completed right okay next one so this is one one thing what you people should aware okay next take a note i mean not not note just observe so sir just now i cover this point for local variables is nothing but temporary data JVM want to provide much support for local variable so JVM will assign default values for local variables no so default values such type of concept applicable only for what only for instance variable static variable but not for local variables right okay and for local variables for local variables JVM for local variables a JVM JVM won't provide default values for local variables JVM won't provide default values JVM won't won't provide default values won't provide default values compulsory we should perform initialization Compulsory we should perform initialization. Compulsory we should perform initialization explicitly. Compulsory we should perform initialization explicitly. We should perform initialization explicitly. Explicitly. Okay. Now observe. Observe this point well. Sir I am taking. Class test, I am taking class test. Now, public static wired main method, string array arcs. Just multiple examples I want, let me take here. Sir, class test, public static wired main method, string array arcs. public static requirements string array action sir int x and now what is this variable this variable is local variable because inside the method i'm declaring local variable system.out.println hello like i'm taking sop of hello hello like this i'm taking sir can you tell either code is going to compile or not If I can take SOP of hello, is it going to compile or not? Yes or no? Sir, for local variable, we are not performing any initialization and JVM won't provide default values. Is it going to compile? 100% the code compiles are not no because for local variables jvm want to provide any default values compulsory we should perform initialization explicitly okay that's why valid the output we are going to get e is hello Because, sir we didn't perform initialization, no problem. But am I using that variable? Am I using? No. Before using, we have to perform initialization. If I am not using, what is the new? of performing initialization are you getting so before using we should perform initialization right that's why no problem at all happily the output by default hello is the output we are going to get clear for all of you right okay next one keep here just observe sir i'm taking class test public static wired main method string array ox Sir, int x, local variable int x, system.out.println of x I am taking, int x, SOP of x. Sir, can you tell, is it valid or invalid? Invalid, immediately compartmentalized by default we are going to get. Are you getting? Compilatory method by default we are going to get S O P of X. Compilatory method saying, what that compilatory method we are going to get? Variable X might not have been initialized. Might not have been initialized like compilatory method by default we are going to get. I hope you people can aware variable X might not have been initialized like so can you please read our statement once again can you please read last point what you took for local variables JVM want to provide default values compulsory we should perform initialization explicitly before using that variable if I'm not using not required to perform initialization right at this point at a compulsory we should perform initialization explicitly continuation before using that variable before using that variable before using that variable that variable that is that is that is if we are not using That is if we are not using, if we are not using then, that is if we are not using then, then it is not required. If we are not using then it is not required. Not required. If we are not using then it is not required. Not required to perform initialization. If we are not using then it is not required to perform initialization. Not required to perform initialization. initialization okay take both examples on a side by side take take clearly int x sop of hello the output we are going to get is hello class test public static void main string array args index sop of x here the comparative method variable x might not have been initialized variable x might not have been initialized initialized might not have been initialized right can i completed right variable x might not have been initialized right That's all sir. There are two more examples I will discuss so that you people can aware clearly right okay. So sir before using we should perform initialization. If I'm not using not required a perform initialization. Best example I'm not using that's why it is the valid. Now next point here see. Sir I'm taking class test. Analyzing carefully about this one class test. Public static wired main method public static wired main string array auxer public static wired main string array auxer sir can you tell what is this one string array auxer command line arguments now i'm taking int x sir i declare a local variable index if if ox dot length if ox dot length greater than zero if ox dot length greater than zero greater than zero sir if we are passing any command line arguments if we are passing any command and arguments So x value will become 10. If we are passing any command line arguments x is the 10. Next system.out.println of x. Sir x is the 10 is 4p of x. Like this I am taking. Do you know? If I can take like this, now I will ask, compiler can you please compile. Sir, if we are passing command line arguments, x dot length greater than 0, command line arguments, x value is 10, and print SOP of x. Now I will ask, compiler can you please compile my code. Like, immediately compiler is going to tell, Durga I can't compile. Arrey, what is the problem man? Like I can ask. Then the compiler is going to tell, Durga, if you are passing command line argument, what is the x value? 10. If you are not passing command line argument, what is the x value? X is initializing sometimes, but not always. Then, what you are trying to print, then if you are not passing command and argument, then what value I have to print? That's why I can't compile like compiler is going to tell. Then I have to convince the compiler saying, Arrey compiler, believe me, definitely at runtime I will pass command-n argument. Definitely at runtime I will pass, not required to worry. Can you please believe, believe me and compile this code like. Then compiler is going to tell, Durga three months back, very innocent person, a person with very innocent face like you, he said nothing but a person came. What he told is, definitely at runtime I will pass command-n arguments like he told. That's why I compiled this code. But what that bloody person did is, at runtime, he didn't pass command line argument. If he didn't pass command line argument, my JVM trying to print x, but x is not having the value, then the JVM in the big trouble, then the JVM called me, gave left and right. Because who is the assistant to the JVM? Compiler is the assistant. If the compiler is not doing the job well, then JVM will be in the trouble. That's why JVM gave left and right to me. That's why I don't want. to take any risk for the sake of stupid programmers like you are getting i don't want to take any risk first if you are not passing command an argument what is the x value let me know then only i can compile it regarding then only i can compile it then immediately compile the method we are going to get saying variable x variable x might not have been initialized might not have been initialized this is the compile time error by default we are going to get can any doubt or any explanation is required okay this is then my compiler not in a position to accept what i will do here is observe this one let me rewrite this code sir class test public static quiet main String array aux. Public static void means string array. String array aux. Now, int x. Int x. If, if aux.length greater than 0. Sir, if you are passing any common line argument, what is the x value? 10. Else, what is the x value? 20. Else x value is the 20. System.out.println of x. S-O-P of x. Like this, I am taking. Sir, can you tell, if I can take like this, is the code is going to compile or not? Is it going to compile or not? Yes, if the compiler rises any error, then I will give left and right to the compiler. Because if I'm passing command and argument, x is the 10. Else, if I'm not passing command and argument, what is the x value? 20. Sir, my x is always initialized. Then how the compiler is going to rise error? No chance at all. Happily the code compiles fine. Now if I can take like this. Sir, Java, Java test, Java test. A, B. A, B. Sir, I'm passing command line argument. What is the answer? What is the output? 10. Because I'm passing command line argument, x is the 10. Next, just Java. Java test that's all. I'm not passing any command line argument. If I'm not passing any command line argument, what is the x value? xia. If you are not passing lspot, x value is nothing but what is the output we will get? 20 by default. Are you getting? Take a very special care. In this case compile time error but there happily the code is going to compile. Now there is some good programming practices. It is never recommended. to perform initialization for local variables inside logical blocks logical block is nothing but if else so like because they may execute or they may not execute correct right at runtime there is no guarantee it is always going to execute sorry if you are not passing command and argument it won't execute are you getting it's never recommended to perform initialization for local variables inside logical blocks clear for all of you right okay this is but so this is the first first conclusion second it is highly recommended to perform initialization for local variables at the time of declaration only suppose if i if i perform initialization here like this index is equal to 0 can you tell me the code is going to compile or not yes so if you are if you are not passing command and argument what the x value zero if you are not passing command and argument if you are passing command and argument x value is the 10 sorry it is highly recommended to keep the x value perform initialization for default for local variable at the time of declaration at least with default values that's why connection con is equal to null are you getting connection con is equal to null default initialization string s is equal to null is nothing but like why we are keeping explicitly null to to overcome this type of problems only clear for a lot right now better to take both examples you These two examples and then I will add conclusions. Add into x. If x dot length greater than 0, x is equal to 10 SOP of x. Variable x might not have been initialized. Extra Java test A, B. What is the output by default you are going to get is 10. Extra Java test output is 20. Java test A, B. Output is 10. Java test output is 20. Completed right? Okay. So take a note about these two conclusions. Note, note, first one. Note first. First one. It is not recommended, it is not recommended, not recommended to perform initialization. Not recommended to perform initialization for local variables. Local variables inside logical blocks. inside logical blocks because because because there is no guarantee because there is no guarantee there is no guarantee for the execution of these blocks because there is no guarantee for the execution of these blocks for the execution of these blocks always at runtime because there is no guarantee for the execution of these blocks always at runtime Always always at runtime Because there is no guarantee for the execution of these blocks always at runtime Always always at runtime Always at runtime okay note to second note second point Sir, here, can I use the term? It is highly recommended, listen, it is highly recommended to perform initialization for local variables at the time of declaration, at least with the default values, at least with the default values. Correct or not highly recommended to perform initialization for local variables at the time of declaration At least with the default values clear for all operated at least with the default values What is the reason for that is nothing matter? Do you know if I can do like this? What is the reason so we can able to prevent this type of compile-time error select because JVM want to provide default values If you are not providing any value if you are trying to access may be a problem. That's why okay add at the time of declaration at the time of declaration at least with the default values at the time of declaration get least with the default values at least at least with the default values at least with the default values default values Okay, this is I Hope is a clear for all of you that you cut this at least with the default default values right now the next Terminology and I take take very very special care about this point Sir, there are few conclusions are there legend sir. I'm taking class a tester Intex is equal to 10 sir. What is this variable? What is this variable? Instance. Instance variable. Now I am taking static into y is equal to 20. What is this variable? Static. Static variable. Static into y is equal to 20. Static variable. Now public static void main method string array args. Int z is equal to 30. What is this variable? Local variable, right? Int z is equal to 30, local variable. Do you know, if the instance variable declared the public, we can access this variable anywhere. If the instance variable declared the private, we can access this variable only within the class. If I access instance variable, if I declare instance variable as the default, then within the current package anywhere if i can access okay instance variable yeah the protected If I access instance variable, it has protected, protected, within the current package anywhere, but outside package only in child classes, right? You know, these are things anyway, based on our modifier, now the corresponding access by default will be there, scope by default will be there. Same rule applicable for this one also. If the static variable declared to the public, static variable declared as private static variable declared as default static variable declared as protected by default applicable right but where we can access local variable where we can access this local variable only within the method is it possible to access outside of the method no suppose if I am taking public can i accept this local variable from outside of the method no the scope of local variable is already fixed fixed that's why public private protected default such type of terminology not applicable for local variables clear right the only applicable modifier the only applicable modifier for local variable is final are getting the only applicable modifier for local variable is final except that you can't use any other modifier by mistake if you are using any other modifier like public private protected like compile time error by default we are going to get is it clear for all of you that okay at this point the only applicable modifier the only applicable modifier The only applicable modifier for local variables is The only applicable modifier for local variables is The only applicable modifier for local variables is For local variables is For local variables is final Final Final, by mistake, by mistake, by mistake, if you are trying to apply, by mistake, if you are trying to apply any other modifier, by mistake, if you are trying to apply any other modifier, if you are trying to apply any other modifier, any other modifier, modifier, then, then, then, we will get compatibility error. if you are trying to apply any other modifier then we will get compile time error then we will get compile time error error error okay just observe this one sir i'm taking class test and i'm taking class test here just observe public static void main method string array ox Okay, yes sir public public Intex is equal to 10 public index is equal 10 are are Private index is equal to 10 Are protected in Texas equal to 10 protected index is equal to 10 Static index is equal to 10 Static index is equal to 10. Next. So transient index is equal to 10. Okay. Volatile index is equal to 10. Sorry if I can take like this In all these cases do you know we are going to get compile time error saying compile time error saying? illegal illegal start of expression are getting legal start of expression like comparative matter we will get i hope exactly here now what i have to do can i can i take like this one final index is equal to 10 valid or invalid huh valid or invalid this one is nothing for effectively valid no problem at all i hope you can aware okay at this point take this example so for local variables Only final is applicable modifier by mistake if you are trying to take any other modifier Compile a matter body follow your contact Final int x is equal to 10. Illegal start of expression. Completed right? I hope is that clear public index is equal to 10 private index is equal to 10 protected index is equal to 10 static index is equal to 10 transient volatile next up final index is equal to 10 illegal start of expression completed right Legal start of expression like. Okay. Next. One more conclusion about these local variables. Okay. Let me talk. Here see. One more conclusion. Suppose. I am taking one variable here. Sir what is this variable? Ray. Instance variable. Sir, I am taking one more variable. What is this variable? Static variable. Public static void main method. String array arcs. What is this variable? Local variable. Right. Okay. Well. Sir, now my point is. If the instance variable, if we are not declaring with any modifier, then by default it is always default. Default means where we can access? Within the current application. If the static variable, if we are not declaring with. any modifier then by default to default we can access this anywhere within the current package so local variable if i'm not declaring with any modifier i'm not declaring with any modifier then can i use that i'm easy to default i know if it is the default where we can access if it is the default where we can access within the current package can i access this z outside of my name oh outside of main possible or not possible not possible that is if we are not declaring with any modifier then by default it is a default this rule is applicable only for what only from static or an instant but not for local variables right clear for all of you if you are not declaring with any modifier then by default it is a default this rule is applicable only for instance variable and static variable but not for local variable local variable scope is always fixed the block only within the current package anywhere within the class anywhere such a type of rules are not applicable for what local variables right take a note note if we are not declaring If we are not declaring If we are not declaring not declaring with any modifier If we are not declaring with any modifier, not declaring with any modifier, any modifier, then, then, then by default, then by default, then by default, it is a but this rule is applicable only only for only for instance under static variables instance under static variables only for instance under static variables static variables but not for but not for but not but not for local variables instance on static variables but not for local local variables but not for local variables local variables that's all completed right so this is what But you have to remember well. What is instance variable? What is the static variable? Local variables. Conclusions. Right. Now let me summarize some more conclusions about that. Summary conclusion first. For instance and static variables. JVM will always provide. Default values but for local variables JVM want to provide default value before using a local variable compulsory We should perform initialization summary point right take take a note our conclusions like better today conclusions conclusions Conclusions first one conclusions common points about all conclusions Okay, yeah the first point for for instance under static variables for instance under static variables instance under static variables jvm will provide default values for instance under static variables jvm will provide default values jvm will provide default values default values and we are not required to perform and we are not required to perform initialization explicitly and we are not required to perform initialization explicitly and we are not required to perform initialization explicitly initialization explicitly explicitly and we are not required to perform initialization explicitly But for local variables, JVM won't provide default values. Compulsory we should perform initialization. before using that variable variable clear right okay now you people have a bit clarity sir what is the instance variable local variable like second point listen listen listen this one sir instance variable do you know part of object correct or not instance variable is the part of object this object can be accessed by multiple threads at a time multiple threads at a time the people who knows synchronization multi-threading concept like now you can aware instance variable part of object a single object can be accessed by multiple threads that's why instance variables are not thread safe thread safety is not there for instance variable because a single object can be accessed by multiple threads Similarly, static variable is also. How many copies of static variable will be created? How many copies? Only one copy at a class level. That a single copy can be accessed by multiple threads at a time. Are you getting multiple threads at a time? That's why it is also thread safety is not there for static variables also. But local variable, do you know? I have M1 method. Index is equal to 10. For every thread, a separate copy of local variable will be created. For every thread, a separate copy of local variable will be created. That's why, if any modification performed by this thread, won't be reflected here. Or if any modification performed by this thread, won't be reflected here. That's why, local variables are thread safe or not thread safe. Observe the difference, right? Add. that instance on the static variables instance on the static variables instance on the static variables can be accessed by instance on the static variables can be accessed by multiple threads can be accessed by multiple threads threads simultaneously can be accessed by multiple threads simultaneously can be accessed by multiple threads simultaneously simultaneously and hence on the hands on and the hands on these are not thread safe and hence these are not thread safe and the hands these are not thread safe not not thread safe not thread safe okay but but in the case of local variables but in the case of local variables but in the case of local variables but in the case of local variables for every thread but in the case of local variables for every thread for every thread for every thread your separate copy will be created for every thread a separate copy will be created for every thread a separate copy will be created will be created will be created and the hence and hence and hence local variables are and the hence Local variables are thread safe Local variables are thread safe Local variables are thread safe Local variables are thread safe thread safe So, sir instance variable ok, of course so type of variable just matter of one minute just observe type of variable is thread safe or not Is the thread safe or not? Sir, first category Ok Instance of variables Instance of variables Second one we covered What is the second one? Static variables Third one, what is the third one? Local, local variables Right Local variable sorry instance variable thread safe or not. Yes, sorry thread safe or not. No No, but local variables are yes. Take this one. Just anyway, just in the table representation beyond that nothing Instance variable type of variable is a thread safe or not instance variable static variable instance variable static variable local variable completed right Next one. Sir, here observe. Every variable in Java, you can take any variable, any variable in Java, compulsory either instance variable or static variable or local variable. Compulsory one of these three. Every variable in Java can be either primitive variable or reference variable. Observe this one. Sir, there are two divisions we covered. First division, either primitive or reference. Second division, instance or static or local. That's why legend. Every variable in Java can be either instance variable. or static variable or local variable one of these three compulsory every variable in java can be either primitive variable or reference variable one of these That's why so the possible variable combinations in Java so instance a primitive or instance reference Next static primitive or static reference. Are you getting? Or local primitive or local reference. Are you getting? The possible types of variables in Java are is nothing but possible combinations of variables are nothing but like. Sir let me talk a simple example. Sir I am taking class test. And I am taking class test. Here, here, int x is equal to 10, static string s is equal to, static string s is equal to Durga. Static string s is equal to Durga. Public static void main, string array ox. Public static void main, string array ox. int array x is equal to or int array y is equal to new int array of 3 on taken. End of this, end of this one. Sir, can you tell, just now we covered. Every variable in Java should be either instance or static or local. Every variable in Java should be either primitive or reference. Can you tell what is about first one? First variable. Is it instance or static or local? Instance. Is it primitive or reference? Primitive. That's why the first variable is instance primitive. Correct or not? Instance primitive. Can you tell what is about first one? tell second variable is it instance or static or local static is it primitive or reference primitive or reference reference reference that's why so static reference static reference is nothing but like next sir what is this one it is a instance or static or local local is it primitive or reference Hey, primitive or reference? Every area in Java is an object only. Okay, that's why reference. Sir, what is this one? Local, local reference. Now, clear for you. Some people may ask, sir, instance variable can be primitive or instance variable can be reference or static variable can be primitive or can be reference like. So, the various possible combinations of variables in Java are is nothing but like this is the example. I hope you people can aware. Okay, add. Next one. Every variable in Java should be. Every variable in Java should be Every variable in Java should be should be should be either instance or static or local Every variable in Java instance are static or local next second second one second second every every second one every every variable in java should be every variable in java should be every variable in java should be should be every variable in java should be either primitive or reference Every variable in Java should be either primitive or reference. should be either primitive or reference or reference reference are you getting right okay this should be either primitive or reference that's next hence hence variable very various hence various various possible combinations of variables in java are hence of various possible combinations hence various possible combinations various possible combinations of variables in java are various possible combinations of variables in java are various possible combinations of variables in java are various possible combinations of variables in java are are are okay yeah in java or take this one take this diagram instance primitive reference static primitive static reference local primitive local reference local primitive and local reference local primitive and the local reference completed right Completed I hope. Not required to keep much explanation. After that take this program or take this example. Completed right? Okay Next one. So this point is a bit important for you people anyway, right? Now the next terminology. Anna take very special care about this one. Here now the next one. If it is related to array, how the behavior keeps happening? Uninitialized arrays. Uninitialized arrays. An initializer erase. An initializer erase. Erase. Okay. So this part also a bit important for you. Sir, I'm declaring an array, but I'm not creating array. Or I'm declaring an array, but I'm not performing initialization. Like what will happen? Just observe. Sir, I'm taking class test. Analysing very carefully class test. Here. Int array x. Can you tell is it instance variable, static variable or local variable? Instance. Is it primitive variable or reference variable? Reference variable because x is array reference. Array is an object that's why reference. Public static void main method. Public static void main method string array arc set. Ok string array arts Here I'm taking take int array x public static void main now my target here is Analogion very very carefully about this one tester t is equal to new test t is equal to new test t here sopr t dot x sop of t dot x under sopr t dot x of 0 Sop of t.x of 0 like I am taking. This is my array. Can you tell what is the output we will get now? Sir, for instance variable, JVM will always provide default value. Can you tell what is the default value of x? reference variable right what is the default value null null reference variable t dot x is nothing but what what the output we will get null null Now you are asking, why null? What is the first element? Null, what is the first element? What thing we are going to get? Ah, runtime exception saying, null pointer exception. I hope, clear right? Sir, now observe. So, if it is instance level. Sir, I am taking instance level. instance level just i declare an array inter a x instance level inter a x now system dot out dot print lm some object reference dot x object reference dot x system dot out dot print lm object reference dot x of 0 sir can you tell what the answer by default we will get now you null second one null pointer exception not required to keep much much explanation right let me execute up to this one minute Here just I am taking inter a x that's all inter a x within the just all these things are not required inter a x test t is equal to new test because instance variable of we can access you know next t dot t dot x t dot x next t dot x of 0 t dot x of 0 okay now observe java c test dot java Next java test Dancer first time is over the answer on a first time null second time or the answer null pointer Excel But now I want to make a small change. What is my change is? Listen, listen this one. Sir, int array x, instead of this line, I am taking int array x is equal to new int array of 3. Oh, the corresponding array object also we created. This is the object. Now system.out.println. obj.x obj.x system.out.print obj.x of 0 can you tell what the answer now obj.x obj.x of 0 what the answer obj.x Because x already initialized that it is an object. That's why OBJ dot x. So x is pointing to array object. Correct or not? X is pointing to array object. That's why whenever we are trying to print any reference variable internally which method will be called to sing method I covered already in the areas so square bracket I class name yet the rate 3e25 a 5 some number has code some number, okay obj.xof0. Have you remembered? Once we create an array, every array element by default initialized with default values. What is the answer? obj.xof0. What is the answer? 0. Because default value for int type. Default value for int type itself is nothing but 0. So here just observe. Instead of just declaration, let me create array object. Let me create array. Array object. The code compiles fine. What is the answer? Yes. Square bracket i at the rate 8D00C6. Some number followed by second one is what? Zero. Clear for all of you. Okay. What heading you took? Can you spell out? Uninitialized arrays. First take this example. Take this example and then summary like you can take. Think very carefully what is the behavior like. So take clear like this after taking this example take these two things I hope exactly here not required to keep much much explanation completed right spop of obj.x are null obj.x of 0 null pointer exception right Next up. Second part, here see. Instead of instance level, let me take static level. Now here see, a small chain. I want to make a small chain. Sir, instead of instance level, I am taking 1. Static level second part. Listen, listen. If it is a static, not required to use object reference, directly we can access. Correct or not? Sir, first one, static int array x. Second part, static int array x is equal to like. Sir, here observe. System.out.println. Directive we can access SOP of X under SOP of X of 0. Static, static variable right. Here also SOP of X. SOP of X of 0. Sir SOP of X. What is the answer? SOP of X. What is the answer? huh for static variables also jvm will provide what default value that's why what answer we are going to get null null next up sop of x of zero sop of x of zero is nothing but what is the answer? Run time exception, null pointer exception. Now you are asking why null? What is your value like? Next here array object created. Now I am trying to print object reference. If I am trying to print object reference, what will happen? Square bracket i at the rate sum 3, 25, a5. Some number, some number, hash code, class name at the rate like. SOP of x of 0. Once we create an array, every element by default initialized with what? Default value. That's what I like. Are you getting? Take this example. Sop of x and sop of x of 0 like completed I hope Sop of x are null. Sop of x are zero. Null pointer exception. Static interi x is equal to new interi. Clear right? This is the second. Second part we have Okay Static level like this next a third one local level thought local local level Local local level sir inside a method. I'm taking on a local level Take very special care here because local the behavior is the different I hope you know local level. Sir if it is a local local inside a method I am taking like this now SOP of x what the answer? Local level what the answer? Compile time error because for local variable JVM won't provide any default values. That's why do you know in both cases we will get compile time error. Saying variable x might not have been variable x might not have been initialized. Okay, like this. Now can you tell, it is the second one. What about in this case? Sir, x already created, x object, right, right, x is initialized. SOP of x, what is the answer? Square bracket i at the rate 325 fi. Because x is already initialized with this object. Now SOP of X of 0, what is the answer? SOP of X of 0, what is the answer? Compiled emitter R0. Aiyo, local error. Compiled emitter R0. Okay, make sure the output by default we are going to get is zero no problem Once we create an array, every array element by default initialized with a default value, whether it is the local array, static array or instance array. x is the local variable, but x of 0 is the part of array object. Array property is going to be dominated. Sir, once we create an array, every array element by default initialized with a default value, irrespective of whether it is instance or local or static. Are you getting at this point? at the point well okay take take this part and then at last i will give the note third one local level third third one local level local level in there a x variable x might not have been initialized Variable x might not have been initialized like comparative method. Might not have been initialized. Next inter ray x is equal to new inter ray of 3 SOP of x. completed right clear right okay now take a note about that point not once we create scenario once we create scenario once once we create scenario every array element once we create scenario every array element every array element by default initializer with every array element by default initializer with by default initializer with default values every array element by default initializer with default values default values irrespective of whether it is instance or static or local correct local correct irrespective of whether it is instance or static or local get it that's all completed right so make sure these things what you people should aware clearly that's all not required to keep much explanation this is about types of variables what we have so instance variable static variable local variable if we are initializing if we are not initializing what is the value and so on that's all variance types of variables