Line | |
---|
1 | package testcode;
|
---|
2 |
|
---|
3 | /**
|
---|
4 | * Test working of varargs, both as static method arg and as constructor arg
|
---|
5 | */
|
---|
6 | public class VarArgs {
|
---|
7 |
|
---|
8 | private Integer thesum;
|
---|
9 |
|
---|
10 | public VarArgs(int... values) {
|
---|
11 | this.thesum = sum(values);
|
---|
12 | }
|
---|
13 |
|
---|
14 | @Override
|
---|
15 | public String toString() {
|
---|
16 | return thesum.toString();
|
---|
17 | }
|
---|
18 |
|
---|
19 | static Integer sum(int... values) {
|
---|
20 | int s = 0;
|
---|
21 | for (int v : values) {
|
---|
22 | s = s + v;
|
---|
23 | }
|
---|
24 | return s;
|
---|
25 | }
|
---|
26 |
|
---|
27 | /**
|
---|
28 | *
|
---|
29 | * @param args the arguments of the call.
|
---|
30 | */
|
---|
31 | static public void main(String[] args) {
|
---|
32 | // simple print
|
---|
33 | System.out.println(sum(1, 2, 3).toString());
|
---|
34 | System.out.println(new VarArgs(1, 2, 3, 4).toString());
|
---|
35 | }
|
---|
36 |
|
---|
37 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.