Google+

Like Us On Facebook


Total Pageviews

74. Constructors






It can be tedious to initialize all the variable in a class each time an object is created. Even when you add convenience methods like DimensionsSet( ) as explained in the second example of previous post Post#73, it would be simpler and more concise to have all the setup done at the time object is first created. Java allows objects to initialize themselves through the use of constructor as follows:

                       Box box1 = new Box(10,10,10);  //Arguments can be passed while creating the object

Constructor is nothing but a method having the same name as the Class. So in order to create a constructor inside Box Class, we have to create a method with the same name of Class i.e. Box( )

Example

Class Box
{
      //Creating a Constructor 
      Box( )
     {

      }

}


Lets implement this on Eclipse IDE by replacing DimensionsSet( ) method with Box( ) constructor:

1. Launch Eclipse, Ensure that 'Third Project' Java is available, Remove the existing code of Box Class and Declare height, width and depth instance variables in Box class as shown below:



2. Create Box( ) constructor with parameters, which receives the arguments that are passed while creating objects in other class as shown below:



3. Create a method volume( ) to calculate the volume of box using the instance variables of Box class and return the calculated volume as shown below and save Box class:


4. Create a new class named 'ConstructorDemo' under 'third_package' of 'Third Project' project as shown below:



 5. Create objects box1 and box2 as instance of Box class, by providing the arguments to be passed during objects creation itself as shown below:



6. Get the results returned by the volume( ) by accessing this method in the print statements as shown below:



7. Sava and Run the 'ConstructorDemo' class as Java Application as shown below:


8. Observe that the output is displayed in console as shown below:



Now I think you understood how the constructor can simplify a Java Program

Download the Project:

Click here to download the Project containing the 'Box' and 'Constructor' class files used in this program. (You can download the project and import it to Eclipse IDE on your machine)

Lets implement Box constructor without parameters on Eclipse IDE

1. Launch Eclipse IDE, Create new Java Project 'Project 001', Create Box Class without main( ) method and Declare the instance variables height, width and depth inside the Box class as shown below:



2. Create a Box( ) constructor for initializing the instance variable of Box class as shown below:



3. Create a volume( ) method for calculating the volume of box using the instance variables of its class as shown below and save the Box class:



4. Create a new class named 'ConstructorsDemo2' under the same project where the Box class is created as shown below:



5. Create objects box1 and box2 as instances of Box class as shown below: (By default Box( ) constructor in Box Class will be called while creating the objects as we have created Box( ) constructor in Box Class and also we've not provided any arguments to be passed while creating the objects in this Class)



6. Call the volume( ) method and print the results returned by the volume( ) method of Box class and print them as shown below:



7. Save and Run the 'ConstructorDemo2' class as Java Application

8. Observe that the output is displayed in console of Eclipse IDE as shown below:



Download this Project:

Click here to download the project containing the 'Box' and 'ConstructorDemo2' class files used in this program. (You can download this project and import into Eclipse IDE on your machine)




Please comment below to feedback or ask questions.

How to use 'this' keyword will be explained in the next post.



6 comments:

Rakesh Hansalia said...

Thanks for sharing

Moni Mathur said...

Hi Arun,

I think The cunstructor without paramenter can be added to third project and cunstructor with paramenter should be kept into different project or project001.
As cunstructor with the paramenter is impacting other classes in the projectthird.

Please correct me if i am wrong :)

Girish Kumar said...

Arun..

Which one is better? constructor with out parameter or constructor with parameter. From my perspective constructor with parameter is better, because you can pass the values when you instantiating the constructor.

Please correct me if i'm wrong.

rajesh a said...

Hi Bro,
Thanks for sharing kt to others ,its helppul like me.
we are begainners for the java (screenshot are very step by step details)

Thanks & Regards,
A Rajesh

rajesh a said...

constructor with parameter is very better method,because its reduce the code reability.
with of my kt is good:)

rajesh a said...

it reduces the code readability.

Blog Index