Tuesday, March 21, 2017

What is a constructor and types of constructors?

When we create a class or struct, its constructor is called. Constructor will have same name as a class or struct and is usually used to initialize the data members of the new object. There are three types of constructors:

Instance constructors are used to create and initialize any instance member variables when we use new expression to create an object of a class.

Static constructors are used to initialize static data or to perform actions that need to be performed only once. It is called automatically before first instance is created or any of the static members are referenced.

Private constructors are only used when a class contains only static members.

No comments:

Post a Comment

Call a function in child component from parent component in React

Consider the following parent component class Parent extends React.Component { constructor(props) { super(props);         thi...