Java中静态变量的声明位置

Java中静态变量只能是成员变量,局部方法中的局部变量除final外不能有任何其他修饰符,例如:

 1 public class Test {
 2     static String x = "1";
 3     static int y = 1;
 4     
 5     public static void main(String args[]) {
 6         static int z = 2;  //报错,无论是普通局部方法还是静态局部方法,内部的局部变量都不能有修饰符
 7 
 8         System.out.println(x + y + z);
 9     }
10     
11     public static void get(){
12         final int m = 2;
13 //        m = 3;  //报错,final修饰的基本类型不可变,String类型不可变,引用类型的引用地址不可改变,但是引用中的内容是可变的
14         final Student s = new Student("1", "abc", 12);
15         Student s2 = new Student("1", "abc", 12);
16 //        s = s2;  //报错
17         s.setAge(13);
18     }
19 }

猜你喜欢

转载自www.cnblogs.com/motong/p/10054843.html