jQuery in Action, 3rd edition
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

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructors" Watch "Constructors" New topic


P Ha

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

Joined: Aug 31, 2004
Posts: 11343

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


"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
I agree. Here's the link: http://aspose.com/file-tools
subject: Constructors
It's not a secret anymore!