`

Difference between static and final static

    博客分类:
  • Java
 
阅读更多

static :我们都知道被static 修饰的变量都是静态变量,对于静态变量在JVM中存在于常量池中,常量池一般在perm heap 中. 对于static 变量的初始化是在类构造器clinit中。

public class A {
	
	public String name="peter";
	public static final int age=100;
	public static int salary = 10000;
	
	static {
		System.out.println("init static block");
	}
}

 

public class Test {
	public static void main(String[] args) {
//		System.out.println(A.age);
		System.out.println(A.salary);
	}
}
result :
init static block
10000

  

 

final static :被final修饰的变量一旦初始化其值或引用都不能修改,final static基本类型的变量的初始化是在类初始化的准备阶段。引用类型的变量初始化也是在类构造器clinit中

public class Test {
	public static void main(String[] args) {
		System.out.println(A.age);
//		System.out.println(A.salary);
	}
}
result :
100

 

分享到:
评论

相关推荐

    C# Game Programming Cookbook for Unity 3D - 2014

    1.1.1 Manager and Controller Scripts...............................2 1.1.2 Script Communication.......................................3 1.1.3 Using the Singleton Pattern in Unity.........................

    微软内部资料-SQL性能优化2

    Troubleshooting server performance-based support calls requires product knowledge, good communication skills, and a proven troubleshooting methodology. In this module we will discuss Microsoft® SQL ...

    Android库项目中的资源ID冲突的解决方法

    2. understanding the difference between main project and library project 在主项目中,资源ID是static final类型的,编译时会直接使用它的值进行替换。在库项目中,资源ID仅被static修饰,编译时不会使用它的值...

    Java邮件开发Fundamentals of the JavaMail API

    * Connections between the JavaMail API and the JavaBeans Activation Framework Objectives By the end of this module you will be able to: * Send and read mail using the JavaMail API * Deal with ...

Global site tag (gtag.js) - Google Analytics