aspose file tools*
The moose likes Beginning Java and the fly likes Constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructors " Watch "Constructors " New topic
Author

Constructors

Jem Edwards
Greenhorn

Joined: Mar 28, 2003
Posts: 22
I'm trying to get a clear understanding of constructors before I start my next exercise. Please tell me if I have a inkling of what I'm talking about. Also could someone "dumb down" constructors even more for me?
My understanding is that a constructor is used only when you create a new object (instantiate an object). For instance: Species dog=new Species(husky,black,10)
A constructor is just a "set method" but the constructor invokes the "set" method at the time the object is created - correct?
The difference is instead of "name of object.set(string,string,int)" it should be "name of class(string,string,int)". For instance:
Species dog=new Species(husky,black,10)
instead of
dog.set (husky,black,10)
Thanks for your guidance.
subhit chauhan
Ranch Hand

Joined: Jun 20, 2002
Posts: 40
Its not necessary to pass the value....
you can create a default constructor also...
Even your class can have no constructors... that condition it will call java.lang.Object's constructor.....
You can set the values by setter methods if you dont want to set it through constructor...
use default...
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

The constructor is the method you call when you create the object. There can be more than one constructor. The default constructor has no arguments. Constructors have no return value.
Don't think of it as a set method. Set methods allow other classes to assign values to your class variables.


Please ignore post, I have no idea what I am talking about.
Donald R. Cossitt
buckaroo
Ranch Hand

Joined: Jan 31, 2003
Posts: 401
Jem;
Check out this tutorial about Class and Inheritance
HTH
doco


doco
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I think you have nicely summarized the basics of constructors. As other people have pointed out, they aren't exactly set() methods, but if it helps you to think of them that way, then go ahead, as long as you understand the differences that you (and the others) listed.


Java API Documentation
The Java Tutorial
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Jem
A constructor is just a "set method" but the constructor invokes the "set" method at the time the object is created - correct?


hum...
I do not agree your statement.
I hope that you need to know the different between method and constructor.
Method can be return anything or nothing you want.
For example

if you without return the same thing you declare
in the method
it will produce complier error

But constructor must return a object reference to the object you declare.
But the object reference you can accept or not
it would not produce complier error
Refer to what situation
The One sample situation is the following
for example



Francis Siu
SCJP, MCDBA
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructors