Constructors

JavaA constructor is a special type of method that is executed when an object is created. The purpose of constructors is to assign values to the fields that are specific to that particular instance of the object. For instance, if you are creating a Car object, you may need to tell the program what brand the car is, what color it is, how fast it is going, and in what direction it is traveling. A constructor allows you to do these things easily when the object is created during program execution.

Saying that a constructor is a method is like saying that the Australian platypus is just another mammal. To understand the platypus, it is important to know how it is different from other mammals. To understand the constructor, it is similarly important to understand how it differs from a method. Any student of Java, especially one studying for certification, needs to know those differences; in this article, I will concretely spell them out. Continue reading

Advertisements