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 about constructer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "about constructer" Watch "about constructer" New topic

about constructer

santhosh kumar vk
Ranch Hand

Joined: Aug 25, 2009
Posts: 151
can you tell what is the use of default constructer and constucter in java
Jaikiran Pai

Joined: Jul 20, 2005
Posts: 10441

While posting meaningful questions, please choose the right forum. I'll move this from Meaningless Drivel to our Beginning Java forum.

[My Blog] [JavaRanch Journal]
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

constructer in java ? or constructor ? Avoid such silly typo as it shows your interest in asking questions.

Constructor's important use is to initialize objects.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46349
Balu Sadhasivam wrote:Avoid such silly typo as it shows your interest in asking questions.
Please!! Lots of people don't spell correctly. I think that comment warrants an apology.
Constructor's important use is to initialize objects.
More precisely, to make sure that all fields in an object are set to appropriate values to establish the class invariant. If the constructor doesn't establish the class invariants, it is incorrectly written.

Probably also a good idea to initialise every field in the constructor, even those where the default value of 0 is appropriate. Also probably a good idea to initialise all reference-type fields so as to avoid null values.
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
To paraphrase what Campbell said, the constructor allows you -- the programmer writing the class -- a change to initialize the object, if needed. All classes must have a constructor, so if you don't have any special initialization needs, you just use the default (no args) constructor. If there's information that must be provided as the object is created, you can create a constructor with parameters used to gather those values. For example, if you had a class Canine, with a "dob" (date of birth) property, and used the default no-args constructor, what would you initialize the date to? If there's a clear default, then you could code the no-args constructor and initialize the value there. If there's not, then you'd code a constructor with a dob parameter, and the calling routine would provide that value.

Hope that helps!
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46349
Agree with Max Rahder, but I am feeling pedantic.

A lot of people forget there is a difference between a default constructor and a no-arguments constructor. A default constructor is a kind of no-arguments constructor (properly defined as one added by the compiler) with an empty body. You can also have no-arguments constructors which have real code inside their bodies.

Example:Maybe not a very useful class, but it is only there as an example.
I agree. Here's the link:
subject: about constructer
It's not a secret anymore!