*
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
Author

Constructors

P Ha
Greenhorn

Joined: Jan 03, 2005
Posts: 1
Why use a constructor. I'm pretty sure I understand how they work but it's still pretty "fuzzy". I just don't understand their primary function I guess. Can someone please help clear this up? Thank you!
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Constructors complete the initialization of new objects. They often accept arguments to use in the initialization. They can pass information to an object's superclass to initialize the superclass first.

Check out a few classes like String and Integer in Sun's API documentation and you'll see how they are used.


Mike Gershman
SCJP 1.4, SCWCD in process
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
I think the primary function of a constructor would be to initilaize some parameters for an object before it is used.
For example in a database object constructor we may define the database connection parameters, before using the database object.

//Example of
public DatabaseConstructor(){
Connection conn=getDatabaseConnection();
Statement stmt=null;
ResultSet rs=null;
}
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

You may want to ready Bruce Eckel's chapter, "Initialization & Cleanup," from Thinking in Java...

http://www.faqs.org/docs/think_java/TIJ306.htm


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructors