wood burning stoves
The moose likes Beginning Java and the fly likes what is the purpose of constructor? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what is the purpose of constructor?" Watch "what is the purpose of constructor?" New topic

what is the purpose of constructor?

nick cheng

Joined: Oct 31, 2004
Posts: 11
Ramaswamy Srinivasan
Ranch Hand

Joined: Aug 31, 2004
Posts: 295
Hi Nick Cheng,

Welcome to JavaRanch.

Talking about the construtors... we can say thatObjects are constrcuted

Whenever this new keyword is used, it is the constructor of the class that gets called.

Normally, constructors are used to initialize the variable state.

One best way to do a ground work on Constructor is to go through through the Chapter 5 of the SCJP Study Guide by our celebrity authors Kathy Sierra and Bert Bates. The concept is explained in excellent, simple way.

One more point, though we don't code for constructors, a default, no arguement constructor is created for each class.

Have a great time with Java.

Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Constructors are very important as they are used to set certain properties and to perform certain tasks. Each object that is created is done using a constructor. When an object is created using the new keyword the constructor for that class is called.

The simplest of constructors is the default constructor. The programmer can explicitly define this constructor. If the default constructor is not defined, the compliler will automatically insert one. When the compiler inserts the default constructor for the programmer, the constructor will not include statements to carry out certain tasks, but the compiler automatically inserts the statements to initialise the members of the class to their default values.

A constructor is always given the same name as the class it is used with.

It should be noted that constructors, unlike methods cannot have a return type and cannot be called by another object.

An example of a constructor in the Book class is:

//Constructor section
Book ( )
//increments number of books created
}//end Constructor Book

The above example in the Book class has a public variable called int noBooks which is static. It will used to keep a count of how many Book objects are created.

Hope the above helps.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Constructors don't create objects.
They don't give instance variables their default values, that happens when the objects are created.
They don't give initialize instance variables user-specified values, that's done by initializers in declarations and initializer blocks.

After all that happens, constructors can do additional setup of the initialized object including processing any arguments specified in the instance creation expression [ new Rectangle( 4, 6, Color.RED ) ], creating other objects needed by this object, and anything else the you want to do.

In thinking about constructors, consider the first statement in the constructor as a separate entity. You can supply this code by coding this(...) or super(..). Otherwise, the compiler puts super() at the beginning of the constructor for you. This first line can pass user-supplied constructor arguments to other constructors of this object or of its parents. The first line of a constructor is executed before the object is created or initialized, while the rest of the constructor is executed afterwards.
[ November 02, 2004: Message edited by: Mike Gershman ]

Mike Gershman
SCJP 1.4, SCWCD in process
nick cheng

Joined: Oct 31, 2004
Posts: 11
I agree. Here's the link: http://aspose.com/file-tools
subject: what is the purpose of constructor?
It's not a secret anymore!