Java中声明常量为什么用static修饰
2015-07-31 17:27
429 查看
在做Android开发的时候,只要查看一些Android源码,不难发现,其中,声明常量都是如下格式:
声明为什么要添加static关键字呢?
之前是这么考虑问题的:定义一个类A,其中包含了用静态变量修饰的常量CONSTANT_A与直接用final修饰的常量CONSTANT_B
在创建多个A的对象时,常量A在内存中只有一份拷贝,而B则有多个拷贝,后者显然使我们不希望看到的。
今天在看Android官网的时候,恰巧看到这一问题:
http://developer.android.com/training/articles/perf-tips.html
原文:
Consider the following declaration at the top of a class:
The compiler generates a class initializer method, called
from the classfile string constant table for
在类第一次被执行的时候,编译器会生成一个初始化方法,这个方法将42的值存入对应的变量intVal,同时通过常量表获得strVal的一个引用。在之后引用这些变量的时候,将进行查询(表)来访问到。
We can improve matters with the "final" keyword:
通过使用final来提升性能:
The class no longer requires a
and accesses to
这样,常量进入了dex文件的静态区初始化部分,不在需要一个初始化方法(来对变量赋值),代码中将直接使用42的常量值,strVal也通过开销低廉的“字符串常量”来代替文件查表。
Note: This optimization applies only to primitive types and
not arbitrary reference types. Still, it's good practice to declare constants
最有的一个小提示:这种右划仅对基本数据类型和String常量有效果,不包括其他引用类型。
看完Google的这段文章,虽然对里面部分名词还是稍有不解,但是了解到static final搭档的另外原因。
<span style="font-size:14px;">private static final String TAG = "FragmentActivity";</span>
声明为什么要添加static关键字呢?
之前是这么考虑问题的:定义一个类A,其中包含了用静态变量修饰的常量CONSTANT_A与直接用final修饰的常量CONSTANT_B
<span style="font-size:14px;">public class A { public static final String CONSTANT_A = "Hello"; public final String CONSTANT_B = "Hello"; }</span>
在创建多个A的对象时,常量A在内存中只有一份拷贝,而B则有多个拷贝,后者显然使我们不希望看到的。
今天在看Android官网的时候,恰巧看到这一问题:
http://developer.android.com/training/articles/perf-tips.html
原文:
Use Static Final For Constants
Consider the following declaration at the top of a class:static int intVal = 42; static String strVal = "Hello, world!";
The compiler generates a class initializer method, called
<clinit>, that is executed when the class is first used. The method stores the value 42 into
intVal, and extracts a reference
from the classfile string constant table for
strVal. When these values are referenced later on, they are accessed with field lookups.
在类第一次被执行的时候,编译器会生成一个初始化方法,这个方法将42的值存入对应的变量intVal,同时通过常量表获得strVal的一个引用。在之后引用这些变量的时候,将进行查询(表)来访问到。
We can improve matters with the "final" keyword:
通过使用final来提升性能:
static final int intVal = 42; static final String strVal = "Hello, world!";
The class no longer requires a
<clinit>method, because the constants go into static field initializers in the dex file. Code that refers to
intValwill use the integer value 42 directly,
and accesses to
strValwill use a relatively inexpensive "string constant" instruction instead of a field lookup.
这样,常量进入了dex文件的静态区初始化部分,不在需要一个初始化方法(来对变量赋值),代码中将直接使用42的常量值,strVal也通过开销低廉的“字符串常量”来代替文件查表。
Note: This optimization applies only to primitive types and
Stringconstants,
not arbitrary reference types. Still, it's good practice to declare constants
static finalwhenever possible.
最有的一个小提示:这种右划仅对基本数据类型和String常量有效果,不包括其他引用类型。
看完Google的这段文章,虽然对里面部分名词还是稍有不解,但是了解到static final搭档的另外原因。
相关文章推荐
- Flex数字类型和Java数字类型的比较
- Java读取excel指定sheet中的各行数据,存入二维数组,包括首行,并打印
- MyEclipse10 离线图文安装SVN插件教程
- Java 泛型
- spring mvc controller 传参
- Java集合类总结
- java把文件夹(目录)子文件夹打成zip文件
- java NIO教程
- Java解析XML文档--SAX
- eclipse查看jar包源代码乱码问题解决
- Java标准流输入输出流In(Out)putStream,ReaderWriter等总结(1)
- eclipse无法打开打开闪退
- 利用freemarker 静态化网页
- Java集合类概述
- Caused by: java.lang.NoSuchFieldError: TRACE
- java 堆栈 静态
- spring, hibernate 事物处理,异常拦截
- Android studio 使用心得(三)—从Eclipse迁移到Android studio
- java缓存(2、ThreadLocal<T>)
- INI文件格式以及Java编码实现读取