Constructor in Java 


is a special member method that will be called implicitly (automatically) by the JVM whenever an object is created for placing user or programmer-defined values in place of default values. A single word constructor is a special member method that will be called automatically whenever an object is created.

The purpose of the constructor is to initialize an object called object initialization. Constructors are mainly created for initializing the object. Initialization is a process of assigning user-defined values at the time of allocation of memory space.

Advantages of constructors in Java
  • A constructor eliminates placing the default values.
  • A constructor eliminates calling the normal method implicitly.

How does Constructor eliminate default values?

Constructor is mainly used for eliminating default values by user-defined values, whenever we create an object of any class then its allocates memory for all the data members and initialize their default values. To eliminate these default values by user-defined values we use a constructor.

Rules or properties of a constructor

  • The constructor will be called automatically when the object is created.
  • The constructor's name must be similar to the name of the class.
  • A constructor should not return any value even void also. Because the basic aim is to place the value in the object. (if we write the return type for the constructor then that constructor will be treated as an ordinary method).
  • Constructor definitions should not be static. Because constructors will be called every time, whenever an object is creating.
  • A constructor should not be private provided an object of one class is created in another class (Constructor can be privately provided an object of one class created in the same class).
  • Constructors will not be inherited from one class to another class (Because every class constructor is created for initializing its own data members).
  • The access specifier of the constructor may or may not be private.
  1. If the access specifier of the constructor is private then an object of the corresponding class can be created in the context of the same class but not in the context of some other class.
  1. If the access specifier of the constructor is not private then an object of the corresponding class can be created both in the same class context and in another class context.

Types of constructors

Based on creating objects in Java constructors are classified into two types. They are

  • Default or no argument Constructor
  • Parameterized constructor.

Default Constructor

A constructor is said to be the default constructor if and only if it never takes any parameters.

If any class does not contain at least one user-defined constructor then the system will create a default constructor at the time of compilation it is known as a system-defined default constructor.

Syntax of default constructor :

                    Class ClassName
                    {
                        ......  // Call default constructor
                        classname ()
                        {
                        Block of statements;  // Initialization
                        }
                        ........
                    }

parameterized constructor

  1. If any constructor contains a list of variable in its signature is known as parameterized constructor. A parameterized constructor takes some parameters.

Syntax for parameterized constructor :           

                Class ClassName         
                {
                    .........
                    ClassName(List of parameters)   // parameterized constructor
                        {
                            ...........
                         }
                            .........
                   }


Important points Related to Parameterized Constructor

  • Whenever we create an object using a parameterized constructor, it must be define parameterized constructor otherwise we will get a compile-time error. Whenever we definethe objects with respect to both parameterized constructor and default constructor, They must be defined by both the constructors.In any class maximum of one default constructor but an 'n' number of parameterized constructors.


  • Progamming is what actually means ?
  • What is software development ?
  • How we categorized software development in different manner.
C Programming
C Plus Plus
Python
Java Development
Web Designing
Javascript
Mysql
Oracle
We cover all the programming concepts in various programming languages, this tutorials are very help full for bigener as well as Experience developer, for the ease of understanding we categorized programming in different manner likewise.