Using Constructor in Java

Creating and using constructor along properties should paid more attention by the Programmer as Using Constructor is inevitable in everywhere in Java coding.
Many postings are given with utmost importance about using constructor in way2java.com and read them as well as to get all the concepts in one example is not possible.

In Java, a class comprises of instance (global) variables, constructors, methods and objects (excluding static blocks and inner classes). These are known as constructs of a class. It’s being a beginner’s code, the instance variables and methods are also discussed briefly.

Instance variable: An instance variable dictates the property to the object of a class. Different objects may have different properties. For example:

public class Car
{
  double petrol;
  int speed;
}

In the above snippet of code, petrol property gives the petrol fuel existing with the Car. The speed variable gives the property at what speed the Car is moving right now.

Method: The method gives the behavior to the Car object by controlling the instance variables. The Car may start with an initial petrol filled with 100.5 liters. The speed property can be changed very often in the course of driving. See how the following code gives 100.5 liters of petrol and 50 KM speed to the Car vehicle at the start.

public class Car
{
  double petrol;
  int speed;

  public void setBehavior(double pet, int spe)
  {
    petrol =  pet;
    speed = spe;
  }
}

The setBehavior() method sets the Car for a petrol given by the local variable pet and for a speed given by spe. How to set the values for pet and spe will be seen in the following program. By calling the setBehavior() method number of times the properties (using instance variables) of a Car can be changed.

Now let us go to a full blown program.

public class Car
{
  double petrol;
  int speed;

  public void setBehavior(double pet, int spe)
  {
    petrol =  pet;
    speed = spe;
  }
  public static void main(String args[])
  {
    Car maruthi = new Car();
    maruthi.setBehavior(100.5, 50);
    System.out.println("First time Petrol filled: " + maruthi.petrol + " and running speed: " + maruthi.speed);
                         // the properties can be changed by calling again setBehavior() method     
    maruthi.setBehavior(200.5, 80);
    System.out.println("Second time Petrol filled: " + maruthi.petrol + " and running speed: " + maruthi.speed);    
  }
}

Using Constructor

The same maruthi Car object is given two times different values by calling the method setBehavior() two times. Here the intention is not much emphasis on variables and methods (they are given in Using Local and Instance Variables and Using Variables from Methods) but is on constructors. See the following statement.

Car maruthi = new Car();

In the above statement Car() is known as constructor. The constructors, default constructor and overloading of constructors are discussed elaborately with example programs and screen outputs in Constructors and Constructor overloading.

Other Constructor related Topics

Leave a Comment

Your email address will not be published.