Monday 29 March 2021

Constructors Part 4 | Static Constructors vs Non Static Constructor |

Static Constructors Vs Non-Static Constructors :

If a constructor is explicitly declared by using a static modifier we call that constructor as static constructor whereas rest of other are non-static constructor only.

Constructors are responsible for initializing fields/variables of a class, static fields are initialized by static constructors and non-static fields are initialized by non-static constructors.

Static constructors are implicitly called whereas non-static constructors must be explicitly called.

Static constructors executes immediately once the execution of a class starts and more over it's the first block of code to run under a class whereas non-static constructors executes only after creating the instance of class as well as each and every time the instance of class is created.

In the life cycle of a class static constructors executes one and only one time whereas non-static constructors executes for zero times if no instances are created and "n" times if "n" instances are created.

 Non-static constructors can be parameterized but static constructors can't have any parameters because  static constructors are implicitly called and more over it's the first block of code to run under class.

Non-static constructors can be overloaded where as static constructors can't be overloaded.

 // C# Program to demonstrate
// how to declare the static
// constructor and non-static
// constructor
using System;
 
class ABC{
   
// Static variable
static int s;
 
// Non-static variable
int ns;
 
// Declaration of
// static constructor
static ABC()
{
    Console.WriteLine("It is static constructor");
}
 
// Declaration of
// non-static constructor
public ABC()
{
    Console.WriteLine("It is non-static constructor");
}
 
// Main Method
static void Main(string[] args)
{
 
    // Static constructor will call implicitly
    // as soon as the class start to execute
    // the first block of code to execute
    // will be static constructor
 
    // Calling non-static constructor
    ABC obj1 = new ABC();
}
}

Output :

It is static constructor
It is non-static constructor

Every class contains an implicit constructor if not defined explicitly and those implicit constructors are defined based on the following criteria :

⟶ Every class except a static class contains an implicit non-static constructor if not defined with an explicit constructors .

⟶ Static constructors are implicitly defined only if that class contains any static fields or else that constructor will not be present at all.

0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (114) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (88) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (19) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (44) Meta (18) MICHIGAN (5) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (741) Python Coding Challenge (190) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses