[Music] welcome to Nerdist technologies and bangarraju and this is the part for video of our constructors so in the part 1 part 2 part 3 our just demonstrating about what is the constructor types of constructors and what is the use of constructors and in this video I wanted to demonstrate about the difference between static constructors and a non static constructors so just want to tell you about the difference between static constructors and non static constructors so in the types of constructors video the part till I was explaining what a static constructor what is the static
constructor if a constructor is explicitly declared by using a static modifier we call that constructor as a static constructor so if a constructor is explicitly declared by using it static modifier we call that constructor as static constant whereas rest of other or non static all rest of other are non static constructors only so if a constructor is explicitly declared by using a static modifier we call that constructor a static constructor whereas rest of other or non static constructors only so the point I'm trying to tell you is let me add a new class here I'll
called as constructors demo constructors demo now in this class I wanted to define two constructors one is a static constructor and one is a non static constructor so static constructors demo and remember this is a static constructor and inside of it I'm writing console dot write line static constructor is called static constructor is called and at the same time one more constructor we'll name this as non static constructor non static constructors can also be called as instance constructors we can also call them as instance constructors fine now this is the static constructor and this is
a non static constructor so that's what I'm telling first point if we declare it explicitly by using a static modifier we call that constructor as a static constructor whereas rest of other or non static so this is static and this is a non static constructor okay and remember what is the need of a constructor as per our discussion as per our discussion earlier we had discussed it what said constructors are responsible for initializing the variables of the class or fields of a class constructors are responsible for initializing the fields variables of a class okay a
responsible for initializing fields or variables of a class fields or variables of a class and now the very important point to remember what you know static fields static fields are initialized by static constructors and non-static fields or initialized by non static constructor so this is the point to remember constructors are responsible for initializing fields of a class so static fields are responsible for so static fields initialized by static constructors and non-static fields are initialized by non static constructor so what I mean to tell is if at all I'm going to declare a field here in
text without a value and static into white without a value both these two fields are going to have a default value so both these two fields are going to have a default value so default values are zero and this is initialized by a non-static constructor and this is initialized by a static constructor so like this the first one is initialized by static constructor and the second one is initialized way non static constructor that's what they're responsible for initializing fields or variables of the class so static fields are initialized by static constructor and non-static fields are
initialized by non static constructor so that is the second important point to remember about a constructor next when will a static constructor execute when will a non static constructor execute static constructors or implicitly called static constructors are implicitly called whereas non static constructors must be explicitly called non static constructors must be explicitly called so this constructors are implicitly called have a look static void main just defining a main because I need an entry point because I need a entry point just I'm only writing the code like console dot readline just to make the screen to
it and now let me run this code if you watch the output static constructor is called see we don't have any code at all see even this red line also let us just comment it we have only main method and this made me three entry point without that we can never run a class so for that reason ever written a main method but when I run this code you notice it it is going to call the static constructor so actually we did not call the static constructor anywhere it is not called anywhere so that's the
point I'm telling you static constructors are implicitly call whereas non-static Constructors must be explicitly called or you must explicitly call non static constructors but static constructors are never called explicitly they are only called implicitly okay fine next the next point static constructor execute immediately once the execution of a class starts static constructor execute immediately once the execution of a class starts and moreover and moreover it's the first block of code to run under a class moreover it is the first block of code to run under a class whereas non-static constructors are called or non static
constructors execute only after creating the instance of class as well as each and every time the instance of class is created so static constructors execute immediately once the execution of class starts just just now I showed you when I run this you can see static constructor is called so immediately once the execution of a class starts static constructor executes and moreover it is the first block of code to run under a class it is the first block of code to run under a class C even if I'm going to write some code in the main
method I'm going to write some code in the main method console dot write line main method is called I'm just hurting console dot write line main method is called run the code entry point is main method and without that main method we can never run the code at all that is entry point but still you notice the code in the main method is not executing first but the code in the static constructor is executing first so when we see this what we think a static constructor can start static constructors reading first so you can make
this an entry point never possible entry point is always main only this is entry point if I comment to this one if at all I'm just going to comment to this particular code here if this code is commented and if you try to run the application it will give you a error what is the error there is no suitable constructor demo is my project name sorry is my class mail and this is my project named constructors demo does not have a suitable static main method so there is no static suitable main method in the class
and without this you can never start a class at all so execution starts with this only no one can change that no one can change that but even though execution is going to start with this okay the first block to run until the class is always a static constructor so execution starts here and before it executes the code in the main it jumps to the static constructor and after jumping to the static constructor executes the code in the static constructor and then comes back to the main method you can just notice outputting in first what
is called static constructor afterwards main matter is code so the point to note is static constructor execute immediately once the execution of the class starts and moreover it is the first block of code to run under a class whereas non static constructors execute only after creating the instance and how many times I have executed the code till now till now we did not see the message a non-static constructor is called the reason is non-static constructor is called only after creating the instance of class why because non-static Constructors must be explicitly called and we call a
constructor while creating the instance ok you can watch the part 1 video of the Constructors so there I was demonstrating so now we must explicitly call a constructor so right now what is your class named constructors demo d1 as equals to a new constructor demo and remember this is a call we are making to the constructor so if you put your mouse on this it will show you this is a class this is a class present under demo project namespace class name is constructor demo but here if you prime mouse is showing a constructor demoed
constructor demo what is it constructed mo dot constructor demo constructor demo is the first thing what is showing is a class name and the second one what is showing is the name of our constructor so we must call a constructor explicitly but its constructor non-static why static constructors are implicitly called around the code you can see non-static constructor is called so this is executing only after creating the instance of a class so whereas non-static constructor exhibits only after creating the instance of a class as well as each and every time the instance of class is
created what are the meaning of it if I create one more instance to my class one more time it is going to call the constructor one more time another instance again one more time like this how many number of instances you go for creating that for the class that many number of times it is going to call the constructor if you create three instances it will call for three times that's what so as well as each and every time the instance of class is created now listen in the lifecycle of a class lifecycle in the
sense the moment we start the execution to end the execution can be considered as one lifecycle the moment you start the execution to enter the execution is considered as one lifecycle in the lifecycle of a class static constructor executes one and only one time whereas non-static constructor execute Farah zero times if no instances are created and Yun times young times if Yin instances are created so the minimum maximum number of times is static constructor executes sin the lifecycle is one and only one but when you come to non-static constructor that will be 0 that will
be 0 if no instances are created and again if any instances are created and if you remember the output when I did not define these constructors when I did not define any of these constructors you can just notice it it too was not calling the constructor so this pros 0 times and now one instance created one time called but now also static construct is called two instances created two times called but now also static constructor is called one time only and you created three instances and it is going to be called three times but now
also static constructor is called one and only one time so this is the other difference what we just have between them and if you want you can just check now Y and X are initialized they are initialized you can print to their values also default values will be 0 console dot write line you can directly print the Y value Y a static member of the class can be directly accessed in a static block a static member of the class can be directly accessed in a static block this is a static member and you can directly
access it under a static block so when you run this it will print the value it is printing the value initialized we did not give value but God initialized and that responsibility is taken by the static constructor to initialize same way console dot write line I cannot print X value like this why I cannot print non-static members of a class can never be directly accessed from a static block it must be accessed by using the class instance because if you just understand we created three instances and the three instances will maintain three copies of ex-cm
Lu but even though it is same value tomorrow the values can be different but today they are the same value but if you are trying to refer to the exe ax if you are trying to refer to the exe are the confusion starts here what the confusion which X are you referring but don't have three copies of y know Y will be one and only one copy but X is going to be three separate copies because non-static field is going to be maintained separately for each instance but static field is going to be one and
only one copy for the whole class so Y is single directly you can call but X is not single so much say D 1 or X printing the value plus D 2 daleks printing the value plus D 3 dot X you can just say 3 zeros here 1 only so actually zeros only no all yeah zeros only we can give our own values but right now what I am trying trying to tell you is this field even if not initialized got a default value and that initialization is done by the static constructor and this field
also has a default value and that initialization is done by this constructor so to prove that I have printer the values to you okay leave it next the next difference non static constructors can be parameterized non static constructors can be parameterized but static constructors cannot have any parameters what is the meaning watch is a non static constructor I want to pass a parameter here what is that int X I want to pass a parameter and if you are going to pass a parameter and that value can be passed from here C 1020 thirty I am
passing the value and inside of my code I'm writing it there's Daleks des drawer X is equals to Y X what is s dot X I'm referring to this X and he ex-miss it refers to the local variable X and now you see D 1 dot X D 2 dot X DT dot access three separate values this proves us what is it these are three separate copies it's not three separate copies but if you just notice that I am able to pass parameters to my non-static constructor can we do the same thing with a static
constructor no it is not possible C into y and I want to write and I want to write I want to assign this particular value Y to this Y okay how do I send the value a secondary but if you notice I'm getting an error there I am getting an error what is the error a static constructor you can read the second line a static constructor must be parameter less it must be parameter less you cannot pass any parameters but why why you cannot pass any parameters okay can be parameterized but static constructors cannot have
any parameters because static constructors are implicitly called they're implicitly called it they are not explicitly called they implicitly called when they are implicitly called if they have any parameters who will pass the value if it is explicitly called if there are any having any parameters I can pass the values I can pass all the values as a developer but implicitly called miss values cannot be Center to it so that is why that cannot be parameterized and Ramba and moreover it's the first block of code to run to run under the class or the first block
of code to run under the class because it is the first block of code to run under the class from where can we send the value if it the second block of code to run the class from the first block I can pass values to that but that is only the first block of the code is only the first blog there is no chance for passing the values for that so these are the two reasons why they cannot be parametrized okay next non-static Constructors can be overloaded whereas static constructors cannot be overloaded what's the meaning
non static constructors can be overloaded and just remember overloading is something that comes into picture based on the parameters overloading is something that comes into picture based on the parameters if if at all we are going to have any parameters based on that parameters overloading will come into picture so here I have a chance to pass parameters I have a chance to pass parameters here so because parameters can be passed here overloading can be done but here there are no parameters no chance to pass parameters so overloading is never allowed in case of the static
constructors non static constructors can be overloaded whereas static constructors cannot be overloaded okay and finally the last more point to understand you what do you know every class contains an implicit constructor if not defined explicitly and those implicit constructors are defined based on the following criteria every class contains an implicit constructor if not defined explicitly and those implicit constructors are defined based on the following criteria okay what are the criteria watch every class except a static class contains and implicit construct every class except a static class contains an implicit I'm sorry implicit non-static constructor contains
an implicit non-static constructor if not defined of it any explicit constructor every class except a static class what is the static class static class means if a class is declared by using a static modifier we call that class a class and a static class can contain only static members in it canta have non static members in it no chance it can contain only static members in it because it can contain only static members in it you cannot have a non static constructor in it also so every class except a static class contains an implicit non
static constructor if not a defined with an explicit constructor then what about static constructor static constructors are implicitly defined only only if the class contains any static fields or else that constructor will not be present static constructors are implicitly defined only if that class contains any static fields or else that constructor will not be present at all that constructor will not be present at all okay if we define explicitly if you're defined explicitly that will be present but here the discussion is not about explicit construct of the discussion is all about implicit constructor when will
implicit constructor come into picture in every class you have a implicit non-static constructor except a static class whereas if you're taught about a static constructor static constructors are not defined implicitly in every class they are defined only if the class contains any static fields no static fields no static constructors also so that is the other important point to remember about the constructors so this is the differences these are the differences between static and non-static constructors so this is all about the constructors what we just have in a class thank you for watching the video for
more videos please subscribe to our YouTube channel nourish item [Music]