File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructors

 
armeen golden
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am very new to java programming and basically trying to teach myself off of a book. i am struggeling to cearly understand what constructors are? what exactly do they do? how to define them? and perhaps any other information that may help to underatnd them better.

thanks.
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructors are very similar to methods, although they are not considered as methods in the Java Language Specification. You declare them very much like you declare a method. The main differences are that a constructor must have the same name as the class it is in and has no return type. The purpose of a constructor is to initialize variables and other state for a new object that is created.

I know this explanation will seem a little hazy, but that is mostly because it is still very acedemic in nature. I suggest that you write a class with a constructor to get a better understand how they work. If you are in a course at school, perhaps the current homework assignment will be a good place to start. There are plenty of examples on the web that you mgiht want to work from as well.

Once you get started writing your own class, please come back with some more questions. The more specific your quetions are the better.

Layne
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A constructor is responsible for creating an object of the class type it belongs to.

In Java it's really easy to do this. Here's a really easy example:

That's it! It's not real exciting of course; this code doesn't "do" anything, except show you want a constructor looks like. It has the same name as the class (and the name is case-sensitive). It looks like a method in the sense that it supports a parameter section and a code section. Both of those are empty in this example, as you can see.

So let's look at a slightly more interesting example:

In this example there's some code in the constructors. Although there are two constructors now, they're distinct to the compiler because their parameter lists are different.

This simple example shows the primary purpose of a compiler; to initialize some, all or no elements of the object at creation time. The println() calls I put in aren't in the best place, by the way, but I wanted to show you something easy that you could play with right now.
 
Mark Van Tuyl
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to emphasize the point, constructors should generally be used only to initialize the state of the object being instantiated. Any processing should take place in methods.
 
armeen golden
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Layne, thank you for the nice explanation. now i'm able to identify a constructor from a method very well.

Michael, the code examples were very useful to visualize the duty of a constructor. is it safe to say that when you define a constructor, you have created an object that is already initialized?

Mark, thanks for the reminder. i understand now that constructors are used to initialize the object only and if anything needs to be done to them, that is done in the method call.
 
Naveen Vooka
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A link that could be useful
http://mindprod.com/jgloss/constructor.html

--------------
Naveen Vooka
www.devsquare.com
DevSquare - Online Application Development
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic