static in Java
来源:互联网 发布:分析家炒股软件 编辑:程序博客网 时间:2024/06/05 11:51
Variables with static
variables can be declared with the static keyword. Example:
static int y=0;
When a variable is declared with the static keyword, its called a class variable. All instances share the same copy of the variable. A class variable can be accessed directly with the class, without the need to creat a instance.
Without the static keyword, it's called instance variable, and each instance of the class has its own copy of the variable.
In the following code, the class t2 has two variables x and y. The y is declared with the static keyword. In the main class t1, we try to manipulate the variables x and y in t2, showing the differences of instance variable and class variable.
class t2 {
int x = 0; // instance variable
static int y=0; // class variable
void setX (int n) { x = n;}
void setY (int n) { y = n;}
int getX () { return x;}
int getY () { return y;}
}
class t1 {
public static void main(String[] arg) {
t2 x1 = new t2();
t2 x2 = new t2();
x1.setX(9);
x2.setX(10);
// each x1 and x2 has separate copies of x
System.out.println( x1.getX() );
System.out.println( x2.getX() );
// class variable can be used directly without a instance of it.
//(if changed to t2.x, it won't compile)
System.out.println( t2.y );
t2.y = 7;
System.out.println( t2.y );
// class variable can be manipulated thru methods as usual
x1.setY(t2.y+1);
System.out.println( x1.getY() );
}
}
Methods with static
Methods declared with static keyword are called class methods. Otherwise they are instance methods. When a method is declared static, it can be called/used without having to create a object first. For example, you can define a collection of math functions in a class, all static, using them like functions. Example:
// A class with a static method
class t2 { static int triple (int n) {return 3*n;}}
class t1 {
public static void main(String[] arg) {
// calling static methods without creating a instance
System.out.println( t2.triple(4) );
// calling static methods thru a instance is also allowed
t2 x1 = new t2();
System.out.println( x1.triple(5) );
}
}
Notes:Methods declared with static cannot access variables declared without static. The following gives a compilation error, unless x is also static.
class t2 {
int x = 3;
static int returnIt () { return x;}
}
class t1 {
public static void main(String[] arg) {
System.out.println( t2.returnIt() );
}
}
Note: There's no such thing as static classses. static in front of class creates compilation error. However, there's a similar idea called abstract classes, with the keyword abstract. Abstract classes cannot be initialized.
- static in Java
- static blocks in java
- Thinking in Java static
- static in java
- Static class in Java
- Static Blocks in Java
- static in java
- Java - Static keyword in Java
- Static and Final In Java
- static in Java and C++
- The Second--Static in java
- Java Static-读Thinking in java
- Java static-摘自thinking in Java
- Can you override Static Methods in Java?
- Dynamic Binding vs Static Binding in Java
- Can you override Static Methods in Java?
- 关于static 的意义---》thinking in java
- static field and method in java
- 周末我也被理发了,但我脸没变!
- Flex编译错误(NOSERVER_FLEX_OUTPUT_FOLDER )
- Excel快捷键汇总
- 使用.NET配置文件
- 快速启动Oracle监听器
- static in Java
- google protobuf 学习
- 与JAVA项目的融合
- 加油啊!
- C#四种排序算法
- 将C++接口封装成C函数
- N阶幻方
- U盘格式变为RAW而不能格式化的最终解决办法记录
- 查询XML文件中的数据