• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor

 
Sherman Hung
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my reading, there are lots of references to CONSTRUCTOR. What exactly does the word constructor mean? Sometimes, it means a class, a object. . .
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, a constructor is used to construct a new object, i.e. an new instance of a class.
This link will help you understand what a constructor is:
JLS 8.8 Constructor Declarations
HIH
 
Pradeepa Battina
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.A constructor is a method with the same name as that of the class.
2.It doesn't have any return type.
3.It is generally used for initialization of instance variables.
This are the basic things about a constructor.
 
Shivaji Marathe
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the Java tuotrial at
http://java.sun.com/docs/books/tutorial/java/javaOO/constructors.html
You can also search the tutorial site for the word Constructor.
HTH
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just adding my $0.02 to what Pradeepa said:
A constructor IS NOT a method. It looks like one (without the return type, though). The difference lies in the way constructor are invoked, a constructor is NEVER invoked the same way as a method. Constructors are invoked by class instance creation expressions (JLS 15.9, Dave Vick's article in the January Newsletter). Constructors are NEVER invoked by method invocation expressions (JLS 15.12 + article in the February Newsletter).
HIH
Sorry for being picky...
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to make things muddier . . . We were discussing the same topic in Beginner.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic