
Constructor is used to initializing objects of a class and allocate appropriate memory to objects. The constructor is used in java programming to assign the default value of instance variables. The use of constructor in Java is as follows: If somebody asks that I want to create a class but nobody should instantiate it, you can say “make the constructor private”. Note: If you apply a private access modifier with a java constructor, you cannot create an object of that class in other classes. By mistake, if you apply any other modifiers with constructor except these four access modifiers, you will get a compile-time error. Non-access modifiers cannot be applied with constructors. These are called access modifiers in java. A constructor can be declared (optionally) as public, protected, and private. Public is an access modifier that indicates that other classes can access the constructor. Here, Rectangle is a class name that must be the same as the name of class that contains constructor. where we can initialize the values of instance variables, local variables, call the methods, and print output. Constructor body which is a block of statements The general syntax is as Access modifiers_name class_name(Parameter_list) Java constructor can be declared inside the class by the following syntax. When we create second object then the constructor is again called during the second time. This means that when an object of a class is created, constructor is called. It is called and executed only once per object. JVM first allocates the memory for variables (objects) and then executes the constructor to initialize instance variables.Ħ. A constructor is automatically called and executed by JVM at the time of object creation. Parameters are local variables to receive value (data) from outside into a constructor.ĥ. Java constructor may or may not contain parameters. The default values for variables are as follows:Ĥ. If we don’t define any constructor inside the class, Java compiler automatically creates a default constructor at compile-time and assigns default values for all variables declared in the class. Whenever we create an object/instance of a class, the constructor will be automatically called by the JVM (Java Virtual Machine).
Suppose you define the method and constructor with the same name as that of the class name then JVM would differentiate between them by using return type.ģ. The constructor should not have any return type even void also because if there is a return type then JVM would consider as a method, not a constructor.Ĭompiler and JVM differentiate constructor and method definitions on the basis of the return type. Constructor’s name must be the same as the name of the class in which it is declared and defined.Ģ. There are the following characteristics of constructor in java. If we try to create an object of the class without specifying any constructor, JVM will create a constructor for us. It is invoked when an instance of a class is created using the new operator.Ĭonstructors can also accept arguments like methods and can be overloaded. Java constructor can perform any action but specially designed to perform initializing actions, such as initializing the instance variables.Ī constructor within a class allows constructing the object of the class at runtime. The sole purpose of the constructor is to initialize the data fields of objects in the class. In other words, a constructor is a special type of method that is used to initialize instance variables in a class.