这一章节我们来讨论一下你不知道的static与final的位置问题。
代码清单
package com.ray.test;
public class StaticAndFinalPositionTest {
private final static int id1 = 0;
private static final int id2 = 0;
// private static int final id3=0;//error
final private static int id3 = 0;
static final private int id4 = 0;
final private int id5 = 0;
private final int id6 = 0;
// private int final id7= 0;//error
static private int id8 = 0;
private static int id9 = 0;
// private int static id10 = 0;//error
private static void test1() {
}
// private void static test2() {//error
// }
static private void test3() {// error
}
final private static void test4() {
}
private final static void test5() {
}
private static final void test6() {
}
// private static void final test7() {//error
// }
private final void test8() {
}
final private void test9() {
}
// private void final test10() {//error
// }
}
从上面的代码,我们可以总结出几点:
(1)static可以放在方法之前,可以放在访问控制符后,但是不能够放到void后面
(2)final跟static一样
(3)当final与static联合一起使用的时候,除了放到void后面,位置也没什么特殊要求
(4)属性域的修饰跟上面的一样原理,从上面的代码我们可以观察得到
总结:这一章节主要讲述你不知道的static与final的位置问题。
这一章节就到这里,谢谢。
-----------------------------------