Constructors

The constructor is one of the most important concepts in the Java programming language. Thus, before we see an example, let's understand what a constructor is.

A constructor executes a block of code whenever an object is created. That means that, whenever we create an object for the class, automatically a block of code will get executed. In other words, a constructor is invoked whenever an object is created.

So where is a constructor used and how do we define it? A constructor should be written, it's just like a method, but the only difference between a method and a constructor is that a constructor will not return any values, and the name of the constructor should always be a class name.

To create a constructor for this class, we will write the following code syntax:

public class constructDemo()
{
//
}

From the preceding code syntax, it is evident that whatever is written in this constructor will be executed whoever creates an object and calls the constructor. If we create an object for the preceding class called constructorDemo, automatically the set of lines present in this block will get executed. That's the main aim of the constructor:

package coreJava;

public class constructDemo {
public constructDemo()
{
System.out.println("I am in the constructor");
}
public-void getdata()
{
System.out.println("I am the method");
}
// will not return value
//name of constructor should be the class name
public static void main(String[] args) {
// TODO Auto-generated method stub
constructDemo cd= new constructDemo();

Whenever the preceding line is executed, the control will automatically check whether there is an explicitly-defined constructor. If it is defined, it will execute the particular block. Whenever one creates an object, a constructor is called in Java.

The output of the preceding code will be:

I am in the constructor

We are not actually creating a constructor for every class but we are specifically bringing in the constructor concept now as, earlier, we did not use any concept when we defined the constructor. Now if we use this command, the program will still run, but this time it will not execute that block. If we do not define any constructor, the compiler will call the default constructor. We might call it an implicit constructor.

We mostly depend on constructors in real-time to initiate objects, or define variables for our program. The constructor and normal methods look similar as they define the access modifier in brackets, but will not accept any return type, but in this case it accepts. Thus, if we write:

public constructDemo()
{
System.out.println("I am in the constructor");
System.out.println("I am in the constructor lecture 1");

}

The output for the preceding code will be:

I am in the constructor
I am in the constructor lecture 1

Therefore, in general, people use the preceding code block to define variables or initiate properties in real-time, and they go ahead with using the constructor.

In the next section, we will look at another constructor that we have in Java.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.26.176