问一个基本问题,static int i=10与int i=10有什么区别?

Ericsson 2002-03-21 03:00:26
谢谢了
...全文
2137 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
一人前端 2002-03-22
  • 打赏
  • 举报
回复
static是静态变量的意思。使用该变量的类,可以不必实例化类。或许就这些吧!
usxue 2002-03-21
  • 打赏
  • 举报
回复
static变量,就是在static的method中也是可以直接访问的!
cxhz_cn 2002-03-21
  • 打赏
  • 举报
回复
调用static的变量不用初始化该变量的类
陈一矛 2002-03-21
  • 打赏
  • 举报
回复
STATIC在整个CLASS中的值是始终保持不变的,和FINAL不一样.给你一个例子,执行一遍你就明白了

//: FinalData.java
// 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 I2 = 99;
// Typical public constant:
public static final int I3 = 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();
//! final Value v4; // Pre-Java 1.1 Error:
// no initializer
// 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 handle
//! fd1.a = new int[3];

fd1.print("fd1");
System.out.println("Creating new FinalData");
FinalData fd2 = new FinalData();
fd1.print("fd1");
fd2.print("fd2");
}
} ///:~

earnest 2002-03-21
  • 打赏
  • 举报
回复
static is class variable,all the object of the class using the variable,any object of the class can change the variable.it not only can be using by the object but also can be using by the class.
GFox 2002-03-21
  • 打赏
  • 举报
回复
可以这样说
static变量是属于类的
非static变量是属于对象的

所有的对象都可以改变static变量的值啊
ddtqfly 2002-03-21
  • 打赏
  • 举报
回复
当然可以拉,它只是静态的变量,又不是常量。如果是
public static final int i=10; 就不能改变拉!
Ericsson 2002-03-21
  • 打赏
  • 举报
回复
dobad(programming)
那后面的对象还可以改变static变量的值吗?
dobad 2002-03-21
  • 打赏
  • 举报
回复
static变量,只在声明该类的第一个对象时创建,在创建该类的其它对象时,不在创建这个变量,而是共享该变量的值。
alafeng 2002-03-21
  • 打赏
  • 举报
回复
static 相当于一个全局的变量!
Ericsson 2002-03-21
  • 打赏
  • 举报
回复
大侠,能不能说清楚点?
怎么个不同?还有其他区别吗?
是 不是static i=10;
这个i的值不能改变了?
ieplayboy 2002-03-21
  • 打赏
  • 举报
回复
初始化顺序不同

23,409

社区成员

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

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