File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes  Java Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark " Java Constructor" Watch " Java Constructor" New topic

Java Constructor


Joined: Dec 18, 2000
Posts: 1
Hi Guy's,

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.
Allen Alchian
Ranch Hand

Joined: Oct 11, 2000
Posts: 83
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!

Angela Poynton
Ranch Hand

Joined: Mar 02, 2000
Posts: 3143
MARTAIN you name does not comply with the Javaranch naming rules which can be found here . Please register again with a valid name.

[This message has been edited by Angela Poynton (edited December 19, 2000).]

Pounding at a thick stone wall won't move it, sometimes, you need to step back to see the way around.
karan roy

Joined: Dec 09, 2000
Posts: 13
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
Terry McKee
Ranch Hand

Joined: Sep 29, 2000
Posts: 175
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.
I agree. Here's the link:
subject: Java Constructor
jQuery in Action, 3rd edition