This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Can anyone help me have a better understanding of constructor? My understanding is that it comes into play as we create a new instance of the object but I just feel that there is more to it than that. Thanks
I'll take a shot at your question. But I'll warn you that I too am a beginner at this Java stuff. That having been said, here goes! The short response: A constructor can initialize values for the object being created. Here's a long example... Assume you've defined a class called Car. In class Car you have defined three constructors. They might look something like this:
In another class you might have code that creates a Car object:
Each of these statements do three things. 1. They create a new variable (chevy and ford) of type Car. This is done by the two words on the left side of the assignment operator (the equals sign). 2. They each create a new Car object. This is done by the "new" operator which calls the appropriate Car() constructor in the class Car. The appropriate constructor is dictated by the number and type of arguments passed to the constructor. The Car constructor will initialize variables of the Car object (set the number of windows and set the color to either a specified color or to the default color, blue). 3. And finally, the assignment operator (the equals sign) will have the variables refer to the newly created objects. So, your constructor has not only created an object, it also initialized some values to better define the object. I'll bet someone more experienced than I could give a much better--and shorter--answer! But this might get you started. Now, look out for the notice about properly registering your name at the JavaRanch!
constructors are basically methods which are invoked automatically once an instance of the class is created. if we do not make a constructor of a class ,the default constructor is automatically created by the compiler. a default constructor is the one which takes no arguements there are some imp. points of constructors- 1)when no construtors are created,a default one is automatically created by the compiler. 2)default constructors are not inherited.if u want to inherit the parameterized constructor too,use the super keyword in the constructor of the sub class. 3)constructors can be overloaded. 4)if there are two constructors in the same class and one constructor calls another one,it can do so using the this. keyword. 5)constructors are automatically invoked when the instance of the class is created. that is what i know about them-hope it helps
One of the comments that were missing from everyones' explanations was the important call to the super class constructor. Though Car does not extend anything explicitly it implicitly extends Object. When you use the new keyword the first statement whether implied or explict is super() (although explicitly parameters could be passed). This is a key concept to understand when dealing with Java for it is the heart of inheritance.