Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructors

 
Jem Edwards
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Donald R. Cossitt
buckaroo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jem;
Check out this tutorial about Class and Inheritance
HTH
doco
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic