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...