File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about constructer

 
santhosh kumar vk
Ranch Hand
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you tell what is the use of default constructer and constucter in java
 
Jaikiran Pai
Marshal
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While posting meaningful questions, please choose the right forum. I'll move this from Meaningless Drivel to our Beginning Java forum.
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 177
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic