• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why do we use a contsructor in a class ?

 
sachin verma
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

Can anybody tell me all reasons to use a constructor in a Class !

rather "Why do we use a constructor in a class ?"
[ June 24, 2008: Message edited by: sachin verma ]
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You use a constructor to initialize the state of a newly-created object of the class.
 
sachin verma
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick reply

But Is it the only reason?
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you use a constructor mostly so you can access that classes methods and attributes parallel to other objects created (as that object instances own).

Other wise to access the methods of a class, you would need to use static methods, or abstract and inherit from that class.

Justin Fox
 
sachin verma
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Clapham
You use a constructor to initialize the state of a newly-created object of the class


Yes I do agree with you.
Can we replace the working of the constructor with "instance block" !\\Line one

I do not agree with the "Line one" in this post because Instance block run right after all the super class's constructor but before the class's constructor (in which there is that instance block).
Instance block can not stand alone itself without the mechanism of constructors.So it can't replace the working of constructors.

Am I Right?

Originally posted by Justin Fox
you use a constructor mostly so you can access that classes methods and attributes parallel to other objects created (as that object instances own).

If it is true, I think it is very deep aspect !
Can you please explore it more?
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a constructor that contains parameters, usually the constructor initializes the instance variables with those parameters.

If you have a constructor that contains parameters and you want to subclass the class in question, you either need to 1) create matching constructor(s) in the subclass or 2) create an additional no arg constructor.

If you don't have a constructor that contains parameters, you do not need to specify a constructor. Java will create an invisible "default no arg constructor" for you.

When you create a new instance of your class, at least one constructor will be used.
 
Angel Rajiv
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I say that we use contructor in a class to force somesome to obey the rule of creating the instance of the class.

is someone disagree with me..?
[ June 25, 2008: Message edited by: Angel Rajiv ]
 
Taariq San
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of your constructor as a cookie cutter. You can make many cookies in the shape and size with the cookie cutter.
Tis such a pity I can't call setSize(GIGANTIC) on my cookies.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic