Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why do we use Constructors in Java ?

 
Vinod Vinu
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do we use Constructors in Java ? What is the purpose of it?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To construct objects? Their purpose seems pretty self-evident: when you create an object, there's obviously a need to initialize the data in that object to a particular known good state. That's what constructors are for.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To ensure everything the object needs is initialized at construction time.

[Damn your quick typing Ernest!]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic