将final数值声明为static和声明为non-static有什么不同呢?

mimi_eleven 2003-11-01 06:15:37
下面的是编译期不能确定其值。
final int i1 = (int)(Math.random() * 20);
static final int i2 = (int)(Math.random() * 20);
这个是在编译期可以确定其值:
final int i3 = 9;
static final int VAL_TWO = 99;

请问在这两种情况下,将final数值声明为static和声明为non-static有什么不同呢?
...全文
32 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
hymarx 2003-12-04
  • 打赏
  • 举报
回复
下面的是编译期不能确定其值。
final int i1 = (int)(Math.random() * 20);//有对象才存在,没个对象创建后保持常量,再创建新对象,就是另外一个值了,在某个特定对象内部有效
static final int i2 = (int)(Math.random() * 20);//全局有效果,不以来对象,整个类加载器上都是常量
这个是在编译期可以确定其值:
final int i3 = 9;
static final int VAL_TWO = 99;

rainboy20024 2003-12-04
  • 打赏
  • 举报
回复
class St{
static final int i=0;
void add(){

System.out.println(i++); //!错误
}
public static void main(String args[]){
for(int i=0;i<100;i++){
St s=new St();
s.add();
}
}//end of main

}
rainboy20024 2003-12-04
  • 打赏
  • 举报
回复
class St{
int i=0;
void add(){
System.out.println(i++);
}
public static void main(String args[]){
for(int i=0;i<100;i++){
St s=new St();
s.add();
}
}//end of main

}//打印出0
class St{
static int i=0;
void add(){
System.out.println(i++);
}
public static void main(String args[]){
for(int i=0;i<100;i++){
St s=new St();
s.add();
}
}//end of main

}//打印0 to 99
Shrewdcat 2003-12-03
  • 打赏
  • 举报
回复
// The effect of final on fields.

class Value {
int i = 1;
}

public class FinalData {
// Can be compile-time constants
final int i1 = 9;
static final int VAL_TWO = 99;
// Typical public constant:
public static final int VAL_THREE = 39;
// Cannot be compile-time constants:
final int i4 = (int)(Math.random()*20);
static final int i5 = (int)(Math.random()*20);

Value v1 = new Value();
final Value v2 = new Value();
static final Value v3 = new Value();
// Arrays:
final int[] a = { 1, 2, 3, 4, 5, 6 };

public void print(String id) {
System.out.println(
id + ": " + "i4 = " + i4 +
", i5 = " + i5);
}
public static void main(String[] args) {
FinalData fd1 = new FinalData();
//! fd1.i1++; // Error: can't change value
fd1.v2.i++; // Object isn't constant!
fd1.v1 = new Value(); // OK -- not final
for(int i = 0; i < fd1.a.length; i++)
fd1.a[i]++; // Object isn't constant!
//! fd1.v2 = new Value(); // Error: Can't
//! fd1.v3 = new Value(); // change reference
//! fd1.a = new int[3];

fd1.print("fd1");
System.out.println("Creating new FinalData");
FinalData fd2 = new FinalData();
fd1.print("fd1");
fd2.print("fd2");
}
} ///:~
clare0peng 2003-12-03
  • 打赏
  • 举报
回复
static是静态的,它属于整个类,直接用类名可以调用
如果没有标明为static则属于对象,必须把类实例化,产生的对象去调用
Shrewdcat 2003-12-03
  • 打赏
  • 举报
回复
Static 型类变量是在第一次载入该类是分配内存空间的,而且是分配一次,对应的内存地址是一直不变的,但内存中存放的值可以改变,在添加final 声明,意味者以后该object Reference 对应的内存中值也不能再发生变化了。

简单说就是内存只有一份,且不能在改变其值。

你的问题应该说是static下,有无final的同还是不同。
zhangjiangbochina 2003-12-03
  • 打赏
  • 举报
回复
final
意味着无法改变,其用途是限定某一个元素为常量.
即不能指向其它的对象:
例:
void simeMethod(final myclass c,final int a[])
{
c.field=7;
a[0]=7;
c=new myclass();//error
a=new int[13]; //error
}
phantomhu 2003-11-01
  • 打赏
  • 举报
回复
static
在类没有实例化就可以调用
haoqingshi 2003-11-01
  • 打赏
  • 举报
回复
static 是静态变量,可以由类直接访问的。而non-static是需要由对象来访问的

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧