A constructor is a function, which would be executed only at the time of creating an object. To receive Constructors Function Homework Help contact us now.
Difference between Constructor and a Function:
- A normal function cannot be called as and when we require to call it, but we cannot call a constructor as and when we want.
- Constructor would be executed only once on the object and that too, only at the time of creating the object. A function can be called on an object as many number of times as we want.
- Name of the function can be any name, but the name of the constructor must and should be the name of the class.
- A constructor will not have any return data type (not even void) where as a function should have a return data type (at least void).
- The key word static cannot be used with constructors. It is applicable to functions and other variables.
- We know that, when ever JVM encounters the new operator, it creates an object of that class and loads all the non-static members of the class into that object and then, the address of the object is assigned to variable.
- But sometimes other than this happen prior to this when JVM encounters the new operator. Here we can see the function of constructors.
A a1 = new A ();
Here A () is a constructor. This statement means, asking the JVM to create object of A .class file by executing the constructor A () defined in A. class file. But note that, till now in our programs we have not defined any constructors in our programs. Then how can JVM execute a constrictor which is not defined. We can have a .java file without constructor, but we can never have .class file without a constructor.