Java Object-Oriented Programming Tutorial on Constructors with Stone River eLearning

Constructor is a concept that Object Oriented Programming has introduced in the world of computer programs; and the use of constructors often minimizes the complexity and the length of a particular code. Imagine giving value of the same variable again and again in different classes before using them in your operations; it would have been so boring and monotonous. However; thanks to constructor, JAVA saves your day by allowing you to initialize all the values that you need in the code inside a single method. That is not all; you do not even have to call this method, as the constructor automatically gets called when the object of the class is invoked.

 

What is a constructor?

A constructor is that method in a class which is automatically called when an object of the class is created and the name of the constructor is same as the name of the class.

Eg:

 


public class length

{

int edge1, edge2;

public length()

{

edge1= 2;

}

public void square()

{

edge2 = edge1*edge1;

System.out.println(“Area of the given figure is “ +edge2);

}}

public class shape

{

length object1 = new length();

object1.square();

}

In the above example, as soon as the object “object1” of the class length is invoked, the constructor of the class automatically gets created and the value of the variable “edge1” is initialized to 2. This value if then used in the method called “square()”  to find the area of the figure.

 

Note:

  1. The name of the constructor must be same as the class name.
  2. A constructor does not have a return type; not even void.
  3. Parameters can be passed to a constructor.
  4. According to the parameters passed, a constructor is of two types:
  5. Default Constructor: The constructor that does not have any parameters passed to it.
  6. Parameterized Constructor: The constructor that has parameters passed to it.
  7. A class can have more than one constructor, however; there will only be one default constructor of any class.
  8. The object for a parameterized and default constructor must be different from each other.

Eg:


public class length

{

int edge1, edge2;

public length()

{

edge1= 2;

}

public length (int x)

{

edge1 = x;

}

public void square()

{

edge2 = edge1*edge1;

System.out.println(“Area of the given figure is “ +edge2);

}}

public class shape

{

length object1 = new length();

length object2 = new length(5);

 

object1.square();

object2.square();

}

It can be noticed that while “length()” is a default constructor of the class length, “length(int x)” is a parameterized constructor. It is important to note that objects of both default and parameterized constructor are created differently.

 

What is a “this” keyword?

“this” keyword is used to refer to the class. In JAVA, this keyword finds importance when a class variable has to be referred from a constructor or a method. In other terms, ”this” keyword finds application when a local variable holds precedence over a global variable. Let us understand the “this” keyword using an example.

Eg:


public class length

{

int x, y;

public length(int x)

{

this.x= x;

}

public void square()

{

y = x*x;

System.out.println(“Area of the given figure is “ +y);

}}

public class shape

{

length object1 = new length(5);

object1.square();

}

You can notice that in the above example, the “this” keyword is used to refer to the class variable “x”. In easier terms, since the name of the class variable is the same as the name of the parameter passed to the constructor “length()”,  using the “this” keyword informs the system that the programmer is trying to refer to the class variable.

 

Built-in method in JAVA

JAVA adds to its reputation of being a programmer friendly and ‘high-utility’ programming language with the built-in methods that it offers to the coders. There are numerous methods in JAVA that have a specified operation, and these methods are accessible directly to the user. The methods are grouped under packages, which can be imported at the start of any JAVA code.  Built-in methods like equals(), max(), min(), length(), etc are extensively used while creating a JAVA based application.

 

One such method which finds great use in JAVA codes is the toString() method of the String class under the lang package. The toString() method is used to return the string representation of an object. As described in the video tutorial, these built-in methods can also be overridden to include a modified operation for them. A big advantage of the toString() method is that; by overriding the toString() method of the Object class, we can return values of the object, ensuring that we do not have to write a lot of code for the purpose. However; these built-in methods are quite useful in their true form and we will be using them in our codes in the upcoming blog posts as well.

 

 

(Visited 441 times, 1 visits today)