Monday, 18 September 2017

How Constructors work.

                            How constructors work

 

Consider the class Conexample which has three fields namely speed, cost and name.

It has two constructors defined. One is a common parameter-less constructor or can also be called as default constructor, the second one is a parameterized constructor having two integer parameters.

ConMain is the name of the main program where the objects of class Conexample are created.




Consider the following picture which demonstrates the execution and inner processing of constructors.



 
      STEPS:

 
(1) Here we create an object of type Conexample named obj1. When the javac compiler encounters the statement “Conexample obj1” it searches for the program named Conexample in the present package or in the present memory location and if the program source file is found it creates a variable obj1 of type Conexample.

(2) When the compiler encounters the code “new Conexample”  the compiler goes to the source file of Conexample and initializes the members/fields which are present outside of a constructor or a function.

(3) When the compiler encounters the code “new Conexample()” the compiler invokes the constructor Conexample() to initialize the fields that are present inside the constructor body.

     (4) Then the reference of the Object created is returned by the constructor to the new keyword.


    (5) The object obj1 is now assigned to the reference returned by the constructor and hence points to the object created and initialized by the constructor as shown in the figure.

Similarly another object obj2 will be created. This comes under a case of parametrized constructor which we shall see in next posts.

Hope this explanation helps. See you again BYE!!

Sunday, 10 September 2017

Constructors in Java

  Constructors in Java 

It can be tedious and sometimes nightmares to initialize all variables in a class each time an instance is created. It would be simpler to have all the initialization done at the time of object creation itself. As the initialization is so common Java allows objects to initialize themselves whenever they are created. This automatic initialization of the object during creation is done by a special function called as constructor. 


So as the above syntax shows , the constructor is a specialized method of a class which has the same name as the class name and the constructor gets automatically gets invoked whenever an object of the class is created and is used to initialize the private data members.

Now, consider a small program which includes the creation of object "obj" of class Printing. 


In the above program, when the control encounters the new keyword memory for the new object is allocated and the  constructor immediately creates and  initializes the object of Printing Class. 
 In simple words, for the above program whenever the main() method is executed, object "obj" of type Printing is created. The moment the object is created the constructor gets executed and you get the output as "Hello World!"

                     Need For Constructors

 In general it is best that all the objects are initialized when they are ceated.
However sometime we cannot initialize the object as we initialize the primitive members.
Consider the simple program given below which try to initialize the fields of class Car just as same as initializing the primitive data types.



In the above programs we can see that java gives an error when we try to initialize "obj1" . So to initialize the members or fields for an Object we use Constructors.

The functioning of the constructor  and more programs involving constructors will be discussed in the next post.




  


             The `this` keyword in java The this keyword in java can be used for different purposes. Some of these are listed below. ...