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); } }
1 Odpowiedź
+ 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.