What Are Java constructors?
A constructor is a special method that is invoked as soon as an object is created using the new operator. In this blog post, I will explaining constructors. Constructors are most commonly used to execute some code that needs to be run as soon as an object of a class is created like setting initial values to the instance variables. Consider the following code snippet: public class Person {private String firstName;private String lastName;public void setPersonDetails(String firstName,String lastName){this.firstName=firstName;this.lastName=lastName;}} Here, there is a Person class. There is a method called setPersonDetails . This method sets values to the firstName and lastName fields. However we need to invoke this method, each time we create a Person object. If we forget to invoke this method, the firstName and lastName fields will not be set . So we need some way for the person details to be automatically set when a Person object is created. Fortunately, Java supports this aut...