What is difference between initializing value through default constructor or parameterized constructor ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is difference between initializing value through default constructor or parameterized constructor ?

Code 1: public class code { int x; public code() { x=2; System.out.println(x); } } public class Program { public static void main(String[] args) { code c = new code(); } } Code 2: public class code { int x; public code(int x1) { this.x=x1; System.out.println(x); } } public class Program { public static void main(String[] args) { code c = new code(6); } }

8th Aug 2020, 4:47 PM
Mayuri Vikram Deshmukh
1 Answer
+ 1
first code: every object will print the same value 2. x is always 2 second code: every object will print the passed value. x depends on the passed value.
8th Aug 2020, 4:52 PM
Bahhaⵣ
Bahhaⵣ - avatar