Monday, 30 September 2013

Methods and Constructor in Java

                 I previously have blogged on Class in Java. We know class contains instance-variables and methods. Both together are called as class members. A class also contains constructors. Before going to constructor we will see, what is method in Java? I guess many of you are familiar with subroutines in C/C++. Subroutines are small codes which are called when you want to execute the business logic present in it. This is in C/C++. When it comes to Java, we have methods to perform the task of subroutines in C/C++. The Java makes, methods more flexible and efficient.


           Methods contain code or business logic which is used when there is requirement. There are four varieties of methods mention below: 

1.     Methods with no parameter and no return type.
2.     Methods with no parameter and have a return type.
3.     Methods with parameter and no return type.
4.     Methods with both parameter and return type.

General Syntax:

           type name(parameter1,parameter2,….,parameterN)
                  //body of method

The type of the method may be any primitive data types or class type. The name is the method name given to method. If method has no type, then void keyword has to be mentioned in the place of type in the general syntax. The void type means that method will not return any value.

Let us see a simple example program. Where we will create a rectangle class which implements method to calculate its area.

package sample.example1627;

public class Rectangle
   double length;
   double breadth;
   double Area()
               return length*breadth;

package sample.example1627;

public class WorkingRectangle
            public static void main(String[] args)
               Rectangle rectangle1=new Rectangle(); // object created
       Rectangle rectangle2=new Rectangle();  // object created
       rectangle1.length=50; //  initializing variable length
       rectangle1.breadth=60; //  initializing variable breadth
       rectangle2.length=100; //  initializing variable length
       rectangle2.breadth=200; //  initializing variable breadth
       System.out.println("Area of rectangle object1 "+rectangle1.Area());
       System.out.println("Area of rectangle object2 "+rectangle2.Area());

Area of rectangle object1 3000.0
Area of rectangle object2 20000.0

                 Consider the below diagram illustrating method calling.


                 A calling code when executed, makes call to the specified method, that is present in same class or the method present in the different class. The control is transferred to the method and the code present in the body of the method is executed. If the method has a return type then the value obtained after execution and the control both are transferred to the calling code. If suppose method has no return type than simply control is transferred to the calling code. 

Constructors in Java:

                           Consider a circumstances where a class has many instance-variables say more than 20 or 30 or more. In such case it will be tiresome job to initialize instance-variables each time an object is created. To avoid tediousness in this kind of circumstances, java provides us with constructors. 

                          A constructor when defined in a class, initializes objects of its class as soon as a new one is created. So that code creating an object will immediately get fully initialized object for use. 

                         Constructor as the same name as that of the class. It will have no return type because class itself is the implicit return type of the constructor. 

                          Constructor can be of two varieties:

1.     Constructor without parameters.
2.     Constructor with parameters.

Note:  A class can have more than one constructor.   

Example program implementing a parametrized constructor: 
package sample.example1627;

public class EmployeesDetails
   String emp_id;
   String first_name;
   String last_name;
   String DOB;
   String profile;
   double salary;
   String email_id;
public EmployeesDetails(String emp_id, String first_name, String last_name,
                        String dOB, String profile, double salary, String email_id)
            this.emp_id = emp_id;
            this.first_name = first_name;
            this.last_name = last_name;
            DOB = dOB;
            this.profile = profile;
            this.salary = salary;
            this.email_id = email_id;

package sample.example1627;

public class EmployeeDemo
   public static void main(String[] args)
               EmployeesDetails rama=new EmployeesDetails("EMP0010", "Rama", "Roy", "10-01-1988", "Manager", 30000, "");
               EmployeesDetails raju=new EmployeesDetails("EMP0011", "Raju", "Mohan", "16-04-1989", "Assitant Manager", 25000, "");
               // display rama's details
               System.out.println("Employee ID: "+rama.emp_id);
               System.out.println("Employee_Name: "+rama.first_name+" "+rama.last_name);
               System.out.println("Profile of Rama: "+rama.profile);
               System.out.println("Salary of Rama: "+rama.salary);
               //display raju's details
               System.out.println("Employee ID: "+raju.emp_id);
               System.out.println("Employee_Name: "+raju.first_name+" "+rama.last_name);
               System.out.println("Profile of Rama: "+raju.profile);
               System.out.println("Salary of Rama: "+raju.salary);
Note: You can see the use of this keyword in above program. The ‘this’ keyword can be used to refer to the current object. In above program this is used to avoid conflict between the class variables and that of the parameters of constructors.