+ 6
what is the difference between constructor and methods?
4 Answers
+ 9
First you need to understand that constructors is a subset of method. So constructor is also a method. But constructor have some special feature than a normal method.
1. A constructor always have same name as class. but anyother method within the class cann't have same name as class.
2. A constructor never have a return type.
3. A constructor always call when you create object with new keyword
4. Class can poses more than one constructor differentiate on the basis of their parameter
5. At any point of time during object creation only one constructor will get called, depend on there parameters pass to constructor method.
Hope this will helpful to you.
Thanks...
+ 8
The constructor is used to initialise an instance of a class. When you create an object, you call the constructor in order to create an object and typically initialise its member variable with the arguments you provide to the constructor.
public class Object {
private double x;
private double y;
public Object(double x, double y) {
this.x = x;
this.y = y;
Object myObject = new Object (x,y);
Methods are you know... They run code whenever they are called.
Hope this was the answer you were looking for.
+ 4
1)constructor don't have return type
methods have return type.
2)name of the constructor should be same as class name
methods name can be anything(except java keywords)
0
Hello everybody. I have one small question. Can we create an object whithout have its constructor? thanks