Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why do we use Constructors in Java ?  RSS feed

 
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
Sheriff
Posts: 24215
37
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
Boost this thread!