• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructors

 
P Ha
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may want to ready Bruce Eckel's chapter, "Initialization & Cleanup," from Thinking in Java...

http://www.faqs.org/docs/think_java/TIJ306.htm
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic