+ 2

Does it mean that every program must have a constructor or the program won't run or can give error??

So if we don't provide a constructor, java creates a default constructor. Also Java being object oriented.. Does it mean we have to have a constructor default or otherwise .??

8th Apr 2019, 7:10 PM
Gayatri Palkar
Gayatri Palkar - avatar
2 odpowiedzi
+ 9
Yes if you dont put a constructor into your class the compiler will put a default constructor there for you. A defaut contructor looks like a no-arg constructor but they are diffrent from one another. theres 3 types. Default : Class(){}; No-arg. : Class(){Some code}; parameterized : Class(int i, int j){};
8th Apr 2019, 8:17 PM
D_Stark
D_Stark - avatar
+ 2
Miss Gayatri Palkar If we not create a constructor . Compiler create a default constructor.. The question is why it create default constructor The reason is -constructor provide default value of data member And second -it reutrn the corrent object refference id. class A { int a; A(A this) { this.a=0; return this; } public static void main(String ... A1) { A c=new A(address of object); System.out.println(c.a); } } Now try to understand the defination of this reference variable This is a reference variable of a class type whic is decleared as a parameter in each instance function available in class
8th Apr 2019, 8:09 PM
Arun Tomar
Arun Tomar - avatar