Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "constructors" Watch "constructors" New topic


armeen golden

Joined: Oct 02, 2005
Posts: 8
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.

Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.


Java API Documentation
The Java Tutorial
Michael Ernest
High Plains Drifter

Joined: Oct 25, 2000
Posts: 7292

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.

Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Mark Van Tuyl
Ranch Hand

Joined: Mar 22, 2002
Posts: 60
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.

<a href="" target="_blank" rel="nofollow">How To Ask Smart Questions</a>
armeen golden

Joined: Oct 02, 2005
Posts: 8
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

Joined: Oct 13, 2004
Posts: 25
A link that could be useful

Naveen Vooka
DevSquare - Online Application Development
I agree. Here's the link:
subject: constructors
It's not a secret anymore!