+ 2

Confusion in creating java objects.

sometimes we code for creating object of class like Car objCar = new Car (): but sometimes we have to put zero on braces like Car objCar = new Car (0): please explain the difference

13th Oct 2018, 5:13 AM
S.K
3 odpowiedzi
+ 6
the first one is using a no argument constructor and the second one is using a constructor with an argument. it's just like passing an argument to a method. when the object is being created, the object will use the input you gave it (0 in your case) in some way to initialise the state of the object.
13th Oct 2018, 8:58 AM
Steven Trippier
Steven Trippier - avatar
+ 2
It depends from the constructor: public Car() { } or public Car(int i) { }
13th Oct 2018, 5:35 AM
Daniele Bonomi
Daniele Bonomi - avatar
+ 1
🤔
13th Oct 2018, 5:37 AM
S.K