File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Why do we use a contsructor in a class ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why do we use a contsructor in a class ?" Watch "Why do we use a contsructor in a class ?" New topic
Author

Why do we use a contsructor in a class ?

sachin verma
Ranch Hand

Joined: Mar 25, 2008
Posts: 177
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 ]

SCJP || SCWCD

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18655
    
    8

You use a constructor to initialize the state of a newly-created object of the class.
sachin verma
Ranch Hand

Joined: Mar 25, 2008
Posts: 177
Thanks for the quick reply

But Is it the only reason?
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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


You down with OOP? Yeah you know me!
sachin verma
Ranch Hand

Joined: Mar 25, 2008
Posts: 177
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

Joined: Jul 22, 2000
Posts: 9047
    
  10
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.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Angel Rajiv
Greenhorn

Joined: Apr 02, 2008
Posts: 4
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 ]

Rajiv Ranjan
Taariq San
Ranch Hand

Joined: Nov 20, 2007
Posts: 192
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why do we use a contsructor in a class ?