Different kinds of Variables in a class | C#.NET Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to nurse technologies and bangarraju and in this video I wanted to demonstrate about the different kinds of variables a class can have and their behavior okay so basically we know what is a variable a variable is something which we use for storing a value so whenever we want to store some values inside a class we just use the concept of variables for storing the values so now when you're going to declare a variable in a class there are four different kinds of variables what you can declare inside a class there are four different kinds of variables what we can declare in a class and the behavior varies so today let us understand about the various kinds of variables in a class and how do they behave okay so basically what are the four kinds of variables first one non static variables second one static variables third one constant variables and fourth one read-only variables select this we can just declare four different kinds of variables in a class so first let us understand what is a non-static variable what is a static variable so first what is a static variable what is the static variable if at all a variable is a declared if at all a variable is declared by using a static modifier we call that as a static variable so without that if you declare a variable directly and that is just going to be a normal non static variable okay just watch see I open the project here and there is a class here and here I am going to declare a variable int X and remember if you have to simply declare a variable like this this is nothing but an instance variable or a non-static variable is nothing but a non-static or instance variable but as if it all I want to go for any static variable reservation declare static int one there is already declared a static variable so what is the static variable if a variable is explicitly declared by user static modifier we call that variable as a static variable one more way how you can declare a static variable is into jet is also static is also static if a variable is declared inside any static block then that variable is also a static variable okay so first if a variable is explicitly declared by using the static modifier or else if a variable is declared under any static block then those variables are static then those variables are static whereas rest of the other are non static where a rest of the other or non static and now we understand three variables in this particular program XYZ X is instance or non static Y is static judge is also static so this is non static and this is static so there's a static variable and this is a non-static variable okay judge is also static variable a variable declared under any static block is also a static variable okay well I am just going to assign some values to them is equals to 100 is equals to 200 okay so two variables declared and assign with some values now come to main in the main I want to print the value of x and y if you want to print the value of y you can straight away say console dot write line y you can straight away say console dot write line Y okay but you cannot say console dot right Linux you cannot say console dot right Linux that is an e is the memory for that X is going to be allocated only if we create the instance of the but coming to why why does not require instance of a class so reason a static variable is initialized immediately once the execution of a class starts though no requirement for creating the instance you do not have any requirement for creating the instance so directly we can bring the y-value watch it you can see printing new the y-value right now so what is the y-value 200 but try printing x value it won't print you it won't print you so when you try if you just check out the error the error will be object references required for the non-static failed method or a property program dot X means it is telling you an instance is required for accessing this X okay why you require the instance first thing is the memory of this particular X gets allocated only after creating the instance until and unless you create the instance memory will not be allocated for the X so because the memory is not allocated for this X until the instance is created it is not possible to print the x value directly okay that's where static methods will execute without any instance but non-static members cannot be accessed or executed without the instance that is the reason why whenever you are trying to consume a non-static method from a static block always it tells you you must create the instance if you don't create the instance the memory is not allocated for X but Y does not require instance Y it is a static number static members of a class doesn't require any instance of the class non-static method class requires instance of the class ok the important point to notice static numbers of a class or doesn't require the instance of class for initialization or execution else whereas non-static numbers of a class require instance of class both for initialization execution so that the first important thing to note static members of the class doesn't require the instance of the class whereas non-static numbers requires the instance of a cast this is the reason why you cannot print this x value until and unless we create the instance of plants so first what to do program p1 is equals to a new program creating instance when you create the instance then the memory is allocated for this particular X now so p1 dot X this is how you should access the value of x but understand to print the Y value instance is not required on the top of it only I'm writing console dot write line Y but I cannot write console dot write line X it should be creating the instance and instance dot X that's how you should call so what I'm trying to tell you now static variables of a class are initialized immediately once the execution of class starts whereas instance variables are initialized only after creating the class instance as well as each and every time the instance of class is created as well as each and every time the instance of class is created what I mean to tell you is watch actually one for execution of the program starts from main method once the execution of the program starts where it starts main method when it starts at the main immediately the memory of Y is going to be allocated so what happens you know internally memory is allocated for Y storing the value so automatically once the execution starts memory is allocated for the Y stores the value and this is why but understand memory of X is not allocated when is it allocated only after creating the instance so what to do now you are required to create a instance and we are doing it C program P 1 is a constant new program so when I create the instance now now it is going to allocate the memory for X and after allocating the memory for X it will store the value of exe and this is your X is how it happens so what I am trying to tell you here is you created one instance now let us create another instance program P 2 is equals to a new program 1 I could another instance internally one more time the memory is allocated and you will find one more copy else so internally there are two copies of this X now and you notice it what is this this is going to be our first instance nothing but v1 and this is our second instance wait two instances created and the first instance where your copy of X and the second instance again we have a copy of X so now if you understand the last point static variables of the class are initialized immediately once the execution of class starts once the execution starts Y is automatically initialized no need to create any instance at all whereas instance variables are initialized only after creating the instance of class as well as each and every time the instance of class is created the meaning is you notice it till you instance is created so two times memory is allocated to instance is created as so two times the memory has been allocated for you in this place so this is how the behavior of your instance variables will be present and the behavior of the static variables will be present now understanding I'm using a word here called life cycle of a class or the clyde cycle of a class the point you start the execution to end the execution is called as a life cycle the point you start the execution to end execution is what we call as a life cycle so in the life cycle of the class in the life cycle of a class a static variable is initialized one and only one time in the life cycle of a as a static variable is initialized one and only one time whereas instance variables are initialized initialized for zero times if no instances are created and n times if n instances or create yeah so in the lifecycle of a class a static variable will be initialized one and only one time whereas instance variables are initialized for zero times if no instances are created and n times if an instance are created if your run your classic created instance don't create instance whatever you do Y will be initialized if you run the class Y will be nationalized but coming to this EXA X will not be nice lysed Y X if at all it has to be initialized miss compulsory we need to create the instance now see I am commenting these two statements if I comment is to remember X is not initialized at all if I run this what is initialized only why is initialized X is not initialized now I am creating one instance ok is X initialized now yes how many times one time why why always one time and I am creating one more instance is X in utilized yes how many times two times y two instances but why one time Y will be one time only but X is two x now two instances created two times the memory is allocated so see P 1 dot X plus some space plus P 2 dot X 2 copies of accepted both are printed but coming to Y there is only one and only one copy ok so another important point initialization of instance variables is associated with a instance creation and construct a calling so instance variables can be initialized through the constructor also so instance variables can be initialized to the constructor also why because if you create the instance only instance variables or non static variables will be initialized okay so I am using the word instance variables instance variable is nothing but a non-static non-static or instance okay so instance variable is nothing but a nonstick or otherwise I will change it as non static initialization of non-static variable is associated with instance creation and constructor calling so non static variables can be initialized through the constructor also we can initialize them through the constructor also okay fine what is that I am trying to tell here we are giving X as hundred see two instances created both of them have the same value hundred but I wanted two different values then what you do public program int X now come here and simply set this dot X is equals to X so now I can pass values from here so value 50 and value 150 so now if you run that see that x and y values are different they are not same means what happened if you just check out in the memory the value of x will be 50 in the first p1 and 150 in p2 the values are different okay but can't we initialize the static variables through a constructor we can initialize but what happens you know every time you assign a new value it will overwrite the existing value will not maintain separate copies first you send one value in slices you send another value overrides existing value every time it keeps on overriding the existing values so generally we don't Denise lies the static variable so through constructor it'll what we initialize only instance variables are initialized through construct okay so this is the behavior of instance variables and static variables okay well so that the first thing non-static or instance and static variables third one what is the third one constant what is the constant if a variable is declared by using the keyboard Co NS t we call it as a constant variable we call it as a constant variable and these constant variables cannot be modified once after their declaration so its must to initialize constant variables at the time of declaration only in the sense suppose I want to declare a constant variable PI in my program how do you declare constant load pi is equals to 3 point 1 4 f so f is it required yes why decimal values are by default treated as double decimal values are by default a teraz double so if you want to represent the value as a float we should suffix that value with EF if you don't suffix evaluate Li of you get a compile time error okay it is compulsory to suffix the character chef here to tell this is a float value not a double value okay fine constant load pi is equals to 3 point 1 4 f the come let's come to the program in the same program on declaring constant float okay watch it at pi what is that you here it is a must to initialize constant variables at the time of declaration only what is it must must be mandatory it is mandatory to initialize the variable so right now constant float PI if you declare like this you will get an error a constant will requires the value to be provided you require a value to be provided okay so must M should I should have sent values equals to 3.14 I did not use the offer I did not use the up there C error what are their retro little means value generally value we called as a lateral lateral of type double cannot be implicitly converted to type float use n you have suffix to create a lateral of this time it is telling you we should use an F suffix to use a little of this time so yeah now Noel okay fine now you understand how many call instances of the class are created two instances so now how many copies of X are there two copies two copies okay it is not required to initialize X with value hundred because we are initializing true constructor I'm deleting it how many copies of X are there two copies already visa under p1 one copy under p2 another copy how many copy of why are they how many copies of Y are the one and only one copy why because it is initialized one and only one time in the lifecycle of a class how many copies of i1 now how many copies of PI will be there coming to PI like instance we do not have multiple copies you will have one copy only like static means it's not going to create you to copy see it's not going to create two two copies here it will creative one and only one copy why not two copies simple because two instance is created two copies creator means ten instances created ten copies are created okay but can we modify this value no we cannot modify the value then we cannot modify the value it is wasting the resources creating ten copies of this particular pie in the memory understand you are wasting the resources now why you cannot modify this value at all then there is no chance of modifying the value so wasting or allocating the memory for ten times is completely going to waste as the memory so to overcome the problem it is not going to create your multiple copies how many copies are created you know one and only one then when is it going to be created when is it going to be located see the behavior of a constant variable will be similar to the behavior of static understand the behavior of constant variables will be similar to the behavior of static variables that is initialized one and only one time in the lifecycle of a class lifecycle of a class and doesn't require the instance of class so understand the behavior of constant variables will be similar to the behavior of static variables that is initialized one and only one time in the lifecycle of a class and does not require the instance of class per axis accessing or initializing so when is it initialized immediately once the execution of class starts why already we discussed on static variable static variables are initialized immediately once the class execution starts now I am telling you constant variables also behave in the same way so now once the execution starts why initialize no in the same way once the execution starts your pie also will be initialized 3.14 by initialized it is not at all associated or linker with the instances of class it is only going to it is only going to allocate the memory without to the class instance support so once we start the execution automatically why is initialized PI is initialized I will prove you watch I am trying to print the value of pi here you can understand now I will comment all this code prints ey you príncipe value this proves what a constant and a static does not require the instance of plus okay but instance variable is not like this compulsory you require to create the instance of class if you want to access an instance or non static variable okay now you have a doubt what is the doubt if a boat are behaving in the same way what is the difference between constant and static then the only difference between a static and constant variable is static variables can be modified whereas constant variables cannot be modified a deer you can modify a static variable but you can never modify a constant variable static can be modified but constant variables cannot be modified okay this is about your constants next read on what is a read-only a variable is declared by using the read-only keyboard we call that variable as a read only variable and these variables also cannot be modified cannot be modified like constants but after initialization understand here when I was talking about constant I said you constant variables cannot be modified after declaration but read-only variables cannot be modified after initialization what is that it's not compulsory to initialize a read only variable at the time of declaration they can also be initialized under the construct they can also be initialized under the constructor but constant will not give you a chance constant declaration time only should I sign read only not required you can assign it later you can assign it later watch read only bull flag I did not initialize the variable no errors in the code no error in the code I did not get any error okay so now if you just understand I can initialize it later then I can initialize I can initialize it under a constructor also so come here I am writing it this dot flag is equals to throw this dot flag is equals to true so where is it initialized inside a constructor but constant is in each class at the time of declaration on okay fine know how many copies of flag are going to be present how many copies of flag are going to be present two copies okay why two copies you cannot modify the value no we cannot modify the value but two copies will be maintained well you know because the behavior of read-only variables will be similar to the behavior of non static variables that is initialized only after creating the instance of class and once per each instance of the class created the behavior of read-only variables will is similar to the behavior of non static variables initialized only after creating the instance of class and once for each instance of the class created okay so just like hex now you have two copies of flag now not one copy there are two copies of flag for you why two copies two instance is created so two copies but the doubt which arises in the mind is two copies created but under the two copies the value is it true so you cannot change it again because after initialization the value cannot be changed again but unnecessary wasting the resources or memory now why two copies now see what is it is just to understand are given your static value is not right what is it what you lose just like X comma bull flak and what will say this dot plug is equals to flag now you're passing the x value no same way you can pass the flag value comma true and here comma false you just notice it in the memory the memory is allocated for the flag here and again for the flag what value true your flag what value false raise your flag so you notice it now the flag value is it true in the first instance and the flag value is false in the second instance but understand you cannot modify the flag value after initialization this value cannot be modified can I modify X value yes but can I modify flag value means no you cannot try P one first let me print the flag value consoled dot write line P 1 dot flag plus some space P 2 dot flag Twin Falls can I modify the x-value P 1 dot X is equals to 500 no error no error in the code but P 1 dot flag is equals to false error why are already said you read only variables cannot be modified after initialize it what is it telling you it read only field it cannot be assigned it or you cannot s any value where you can assign except in a constructor or variable initializer option number 1 declaration time s any value option number 2 % value it pad from these two you cannot again assign value to it so that's the meaning of it so now this cannot be modified you understand this cannot be modified so if you just understand whatever Stelling is the behavior of read-only variables will be similar to the behavior of non-static variables that is initialized only after creating the instance of class and once for each instance of the class is created ok then you have it out then what is the difference between read-only and a non-static that when I showed you right now what is it non-static variables can be modified but not read-only variables the only difference between read-only and instance variables is instance variables can be modified but not read-only variables you cannot modify read-only variables what you can modify you can modify instance or non static variables but you cannot modify the read-only variables so that is the only difference between this X and flag same way Y n PI Y can be modified by cannot be modified yet can be modified flag cannot be modified so what is the difference between constant and read-only simple here constant is a single copy for the whole class a read-only is a copy for each instance constant is a fixed value for the whole class but read-only is a fixed value specific to an instance constant variable is a fixed value for the whole class whereas read only variable is a fixed value specific to an instance of class it is specific to an instance of class so that is the difference what you have between constants and read-only variables okay so this is about this is about the four different categorizations of the variables so first one instance variable what is an instance variable instance variable also called as non-static variable what is it maintains one copy for each instance of the class created okay and initializes only if the instance is created if you create an instances n copies zero instances zero copies static maintains one value for the whole class maintains one value for the whole class okay and can be modified you can modify the value but will not create multiple copies will not create your multiple copies initializes one and only one time third one constant or the constant these variables cannot be modified after declaration so you must assign the value at the time of declaration only okay and very importantly you cannot modify the value maintains one and only one copy throughout the lifecycle of the class and lastly read-only and these variables also cannot be modified but after initialization not after declaration this declaration time no need to give a value initialization after initialization you cannot modify the value again and maintains a copy for each instance if you create under instances under copies zero instances Z copies okay and here also once the initialization is done it cannot be modified so this is how you can just declare the variables so for different categories of the variables what register so which one is better we cannot conclude that here the reason is each and every variable is going to be required in a different scenario I was trying to tell you the characteristics of each and every variable so when you are developing applications depending on the requirement you need to decide their failure to go with it instance or constant or static or read-only you should analyze the characteristics of each and every attribute of your class or variable of your class whether you want a single copy or multiple copies single copy means either a static can be their constant can be their multiple copies means non static read-only second thing you required to take modifiable or non-modifiable single copy modifiable static single copy non-modifiable constant multiple copies modifiable instance multiple copies non-modifiable read-only like this we require to think over and then we need to declare an appropriate variable in the appropriate situation but just to make you understand the difference of their behavior I just showed you about each and everything so make sure when you are writing the program analyze the characteristics of each and every variable and then you just choose the best type of variable in the appropriate situation okay thank you for watching the video for more videos please subscribe to our YouTube channel nourish IT
Info
Channel: Naresh i Technologies
Views: 87,567
Rating: undefined out of 5
Keywords: C#.NET, Bangar Raju, Naresh IT, Learn C#.NET, Hands on C#.NET Training, C#.NET Demo, Online C#.NET Training, C#.NET Tutorial Videos, C#.NET Overview, C#.NET Interview Questions
Id: pVTEwRmjV50
Channel Id: undefined
Length: 36min 2sec (2162 seconds)
Published: Fri Oct 21 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.