例4-5:
class Tom{
final int MAX=100;
static final int MIN=20;
}
public class Example4_5
{
public static void main(String args[]){
System.out.println(Tom.MIN);
Tom cat=new Tom();
int x=0;
x=Tom.MIN+cat.MAX;
System.out.println(x);
}
}
结果:
---------- 运行 ----------
20
120
输出完成 (耗时 0 秒) - 正常终止
例4-6:
class Computer
{
double x,y;
static double max(double a,double b){
return a>b?a:b;
}
}
class Example4_6
{
public static void main(String args[]){
double max=Computer.max(12,45);
System.out.println(max);
}
}
结果:
---------- 运行 ----------
45.0
输出完成 (耗时 0 秒) - 正常终止
本文通过两个Java示例介绍了静态变量与方法的应用。首先展示了一个类中静态与非静态final变量的使用,并演示了如何在main方法中访问这些变量。其次,通过一个具体的例子说明了如何定义并调用静态方法来比较两个数值的大小。

被折叠的 条评论
为什么被折叠?



