Relearning in JAVA: 5)Initialization & cleanup

来源:互联网 发布:windows截图快捷键 编辑:程序博客网 时间:2024/06/07 14:47

Chapter 5.

p184. In a class, all the variables are initialized before any method can be called, even the constructor.

Array initialization and toString:


int[] a = new int[]{1,2,3,4};int[] b = {1,2,3,4};System.out.println(Arrays.toString(a));@Testpublic void test() {    int[] ints= {1,2,3,4};    print(1,2,3,4);    print(ints); print();} private void print(int... args) { System.out.println(Arrays.toString(args)); for (int a : args) {System.out.println(a);}}

Variable argument list makes method overloading more complicated. You should generally only use a variable argument list on one version of an overload method. Or consider not doing it at all.


.

The only safe methods to call inside a constructor are those that are final in the base class.



阅读全文
0 0
原创粉丝点击