Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » 技术文章库  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Java Interface 是常量存放的最佳地点吗?[转]
scottding

十三部落酋长

CJSDN高级会员


发贴: 1054
积分: 101
于 2003-05-02 22:43 user profilesend a private message to usersend email to scottdingsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
由于java interface中声明的字段在编译时会自动加上static final的修饰符,即声明为常量。因而interface通常是存放常量的最佳地点。然而在java的实际应用时却会产生一些问题。

问题的起因有两个,第一,是我们所使用的常量并不是一成不变的,而是相对于变量不能赋值改变。例如我们在一个工程初期定义常量∏=3.14,而由于计算精度的提高我们可能会重新定义∏=3.14159,此时整个项目对此常量的引用都应该做出改变。第二,java是动态语言。与c++之类的静态语言不同,java对一些字段的引用可以在运行期动态进行,这种灵活性是java这样的动态语言的一大优势。也就使得我们在java工程中有时部分内容的改变不用重新编译整个项目,而只需编译改变的部分重新发布就可以改变整个应用。

讲了这么多,你还不知道我要说什么吗?好,我们来看一个简单的例子:

有一个interface A,一个class B,代码如下:


//file A.java
public interface A{
  String name = "bright";
}

//file B.java
public class B{
  public static void main(String[] args){
    System.out.println("Class A's name = " + A.name);
  }
}


够简单吧,好,编译A.java和B.java。

运行,输入java B,显然结果如下:

Class A's name = bright

我们现在修改A.java如下:


//file A.java
public interface A{
  String name = "bright sea";
}


编译A.java后重新运行B class,输入java B,注意:结果如下

Class A's name = bright



为什么不是"Class A's name = bright sea"?让我们使用jdk提供的反编译工具javap反编译B.class看个究竟,输入:javap -c B ,结果如下:


Compiled from B.java
public class B extends java.lang.Object {
public B();
public static void main(java.lang.String[]);
}

Method B()
0 aload_0
1 invokespecial #1 <Method java.lang.Object()>
4 return

Method void main(java.lang.String[])
0 getstatic #2 <Field java.io.PrintStream out>
3 ldc #3 <String "Class A's name = bright">
5 invokevirtual #4 <Method void println(java.lang.String)>
8 return


注意到标号3的代码了吗?由于引用了一个static final 的字段,编译器已经将interface A中name的内容编译进了class B中,而不是对interface A中的name的引用。因此除非我们重新编译class B,interface A中name发生的变化无法在class B中反映。如果这样去做那么java的动态优势就消失殆尽。

解决方案,有两种解决方法。

第一种方法是不再使用常量,将所需字段放入class中声明,并去掉final修饰符。但这种方法存在一定的风险,由于不再是常量着因而在系统运行时有可能被其他类修改其值而发生错误,也就违背了我们设置它为常量的初衷,因而不推荐使用。

第二种方法,将常量放入class中声明,使用class方法来得到此常量的值。为了保持对此常量引用的简单性,我们可以使用一个静态方法。我们将A.java和B.java修改如下:


//file A.java
public class A{
  private static final String name = "bright";
  public static String getName(){
    return name;
  }
}

//file B.java
public class B{
  public static void main(String[] args){
    System.out.println("Class A's name = " + A.getName());
  }
}



同样我们编译A.java和B.java。运行class B,输入java B,显然结果如下:

Class A's name = bright

现在我们修改A.java如下:


//file A.java
public class A{
  private static final String name = "bright";
  public static String getName(){
    return name;
  }
}



我们再次编译A.java后重新运行B class,输入java B:结果如下

Class A's name = bright sea



终于得到了我们想要的结果,我们可以再次反编译B.class看看class B的改变,输入:

javap -c B,结果如下:


Compiled from B.java
public class B extends java.lang.Object {
public B();
public static void main(java.lang.String[]);
}

Method B()
0 aload_0
1 invokespecial #1 <Method java.lang.Object()>
4 return

Method void main(java.lang.String[])
0 getstatic #2 <Field java.io.PrintStream out>
3 new #3 <Class java.lang.StringBuffer>
6 dup
7 invokespecial #4 <Method java.lang.StringBuffer()>
10 ldc #5 <String "Class A's name = ">
12 invokevirtual #6 <Method java.lang.StringBuffer append(java.lang.String)>
15 invokestatic #7 <Method java.lang.String getName()>
18 invokevirtual #6 <Method java.lang.StringBuffer append(java.lang.String)>
21 invokevirtual #8 <Method java.lang.String toString()>
24 invokevirtual #9 <Method void println(java.lang.String)>
27 return




注意标号10至15行的代码,class B中已经变为对A class的getName()方法的引用,当常量name的值改变时我们只需对class A中的常量做修改并重新编译,无需编译整个项目工程我们就能改变整个应用对此常量的引用,即保持了java动态优势又保持了我们使用常量的初衷,因而方法二是一个最佳解决方案。



Blog   Home   @unumu

作者 Re:Java Interface 是常量存放的最佳地点吗?[转] [Re:scottding]
luoq_dl

Love



发贴: 162
积分: 41
于 2003-05-03 00:10 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不总是,我觉得。
常量作为属性,应当归属于对象。
至于归属于对象本身、还是对象实现的接口,应当由具体的应用情况来分析。



作者 Re:Java Interface 是常量存放的最佳地点吗?[转] [Re:scottding]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2003-05-03 00:23 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Hehe, if you recompile A, why not just recompile B as well, then you won't have this trouble at all. Big Smile.

Most of the time I have these cases, these constants are just keys for hashtable, etc, so rarely need to change, since I reference them everywhere through the interface, javabean, jsp, etc.



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code
作者 Re:Java Interface 是常量存放的最佳地点吗?[转] [Re:luoq_dl]
gunrose





发贴: 100
积分: 20
于 2003-05-04 10:12 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我不同意楼主摘录的【文章的观点】(不是楼主的观点,是IBM文章的观点Cool):
我觉得【公用】的常量应该放在接口里,原因是它是公用的,不是属于任何一个类的,或者你另外给这些常量定义一个类,相当于Enum,每个常量都是Enum的一个实例,这样我也同意。
而文章中的问题我觉得是编译的问题,要么是程序员编译的问题,要么是编译器的问题。

他那种使用方法来代替常量的想法,我觉得绝不可取。只想问一句,那还是常量吗?如果有多个常量,每个都写一个这样的方法,可取吗?别人用的时候,知道这是常量还是变量?



作者 Re:Java Interface 是常量存放的最佳地点吗?[转] [Re:scottding]
scottding

十三部落酋长

CJSDN高级会员


发贴: 1054
积分: 101
于 2003-05-08 13:53 user profilesend a private message to usersend email to scottdingsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
这篇文章我也仅仅是粗略的看了一下,
没有具体的进行实践。
找个时间我会仔细研究一下,给出我的结果。



Blog   Home   @unumu

作者 Re:Java Interface 是常量存放的最佳地点吗?[转] [Re:scottding]
photonman





发贴: 30
积分: 0
于 2003-05-10 22:14 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
每种方案都有其适用范围,什么是常量?为什么要用常量?

如果按照文章中所述,仅仅是为了重新编译部分Class使其可以plug-in,那么这个代价是值得的,在此这个name是不是应该是常量值得商榷,还不如放到配置文件重新载入Smile

而往往运用常量,一个是保证整个系统都可以访问,或者是保证其不变性,另外一个也是效率的目的,如果还是用文中所说的通过方法来Access那么效率就没有什么提高。



Mail2Me ->>
MSN:tony_mao@hotmail.com

作者 Re:Java Interface 是常量存放的最佳地点吗?[转] [Re:scottding]
loafer



发贴: 0
积分: 0
于 2003-05-17 10:44 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
个人认为文章提到的内容是基于这样的概念:所谓常量,在这里是指通被运行时相关类统一使用,并保持不变的一个符号值。注意这里是运行时不变,不是任何时候都不变。

floater认为可以通过编译所有相关类来实现这种变化,我认为代价太大,甚至是不可行的。

关于常量与效率的问题,倒没有考虑到,但是否应该执着于这个效率呢?



作者 Re:Java Interface 是常量存放的最佳地点吗?[转] [Re:scottding]
photonman





发贴: 30
积分: 0
于 2003-05-19 10:19 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
If u want to change value without recompile, just write it in deploy files, and reload at proper time.


Mail2Me ->>
MSN:tony_mao@hotmail.com


flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923