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.
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...
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.
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.
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