+ 1

Is it possible to initialize two variables(same datatype and different datatype) in one constructor?

4th Feb 2017, 8:10 AM
Kommoju Sunil Kumar
Kommoju Sunil Kumar - avatar
3 Réponses
+ 3
Are you looking for varargs? public class Program{ // varargs in constructor public Program(int... i) { System.out.println("Constructor:"); for (int j=0; j<i.length; j++) System.out.println(i[j]); } public static void main(String[] args) { Program prog = new Program (1,2,3); Program prog1= new Program (4,5,6,7,8); } }
4th Feb 2017, 6:11 PM
Tashi N
Tashi N - avatar
+ 2
Yes we can! public Program(int i, String str, double d) { }
4th Feb 2017, 6:29 PM
Tashi N
Tashi N - avatar
+ 1
Thank you for your answer Tashi. You had helped me with one question. Another question that is to be answered is can we initialize different datatype variables in the same constructor?
4th Feb 2017, 6:27 PM
Kommoju Sunil Kumar
Kommoju Sunil Kumar - avatar