java 15:静态变量,方法,常量

时间:2022-02-09 15:08:44

我们在上一篇的Circle类中,r(半径)是一个实例变量(instance variable),而实例变量是与对象绑定的;在同一个类之间的不同对象不能共享。例如,当我们 Circle C1=new Circle(); Circle C2=new Circle(); 这样创建出来的两个对象存储在不同的内存空间,有各自的r值,互不影响。那么,如果我们想一个变量能够给所有对象共享,例如要一个变量来计算总的创建了多少个对象应该怎么办呢? 这时候该属性相当是属于整个类的,而不是特定的对象,那么这时候我们需要静态变量。在创建对象的时候,静态变量会被独立出来放在共同的内存空间,这样每个对象对其的修改都会影响到其他变量。

If you want all the instances of a class to share data, use static variables, also known as
class variables. Static variables store values for the variables in a common memory location.
Because of this common location, if one object changes the value of a static variable, all
objects of the same class are affected. Java supports static methods as well as static variables.
Static methods can be called without creating an instance of the class.
例如,我们要用一个变量来统计我们创建了多少个Circle 对象,也就是我们创建一个对象时候,它的值为1.两个时候值为2.......静态变量可以通过类名或者对象名来使用。

public class CircleCount
{
double r=1.0;
static int count=0;
public CircleCount()
{
CircleCount.count++;
}
public CircleCount(double r)
{
this.r=r;
CircleCount.count++;
}
public double getArea()
{
return r*r*Math.PI;
}
public static int getCount()
{
return count;
}
public static void main(String [] agrs)
{
CircleCount c1=new CircleCount();
CircleCount c2=new CircleCount();
CircleCount c3=new CircleCount(3);
System.out.println("count="+CircleCount.getCount());
}

}

这里要注意,开始我getCount函数没有加上static,结果在运行时候出现 “不能在静态上下文中引用非静态方法”,这是因为:

Static variables and methods can be used from instance or static methods in the class. How-
ever, instance variables and methods can be used only from instance methods, not from static
methods, since static variables and methods don’t belong to a particular object. 

静态变量和方法可以在对象或类的静态方法中使用。但是实例变量和方法只能在实例方法中使用,不能在静态变量中使用,因为静态变量和方法不是属于某一特定对象。所以一句话,在static方法中,不可以使用非static的变量或方法,要用的话只能通过创建对象来使用。但在非static的方法中,static跟非static的方法、变量都可以用。

java 15:静态变量,方法,常量

再来看个例子:

public class Foo
{
int i=5;
static int j=2;
public static void main(String [] args)
{

int k=i; //错误,静态方法中不能直接访问非静态变量
m1(); //错误,静态方法中不能访问类的非静态方法
m2(); //正确,静态方法中可以直接使用静态方法
}
public void m1()
{
i=i+j+m2(i,j); //正确,非静态方法中可以使用非静态、静态的变量、方法
}
public static int m2(int i)
{
return (int)(Math.pow(i,j)); //正确,静态方法中只能使用类中定义的静态变量、方法
}


}

但是如果这样改,那么便是可以的:

public class Foo
{
int i=5;
static int j=2;
public void m1()
{
i=i+j+m2(i,j); //非静态方法中可以使用非静态、静态的变量、方法
}
public static int m2(int i)
{
return (int)(Math.pow(i,j)); //静态方法中只能使用类中定义的静态变量、方法
}
public static void main(String [] args)
{
Foo foo=new Foo();
int k=foo.i; //静态方法中要访问非静态变量、方法需要通过对象来使用
foo.m1();
//foo.m2(1,2);
System.out.println("foo.i="+foo.i+",foo.j"+foo.j+",foo.m2="+foo.m2(1));
}

}

怎么决定一个方法或变量是否应该定义成static?

How do you decide whether a variable or method should be an instance one or a static one? A vari-
able or method that is dependent on a specific instance of the class should be an instance variable or
method. A variable or method that is not dependent on a specific instance of the class should be a
static variable or method. For example, every circle has its own radius. Radius is dependent on a spe-
cific circle. Therefore, radius is an instance variable of the Circle class. Since the getArea
method is dependent on a specific circle, it is an instance method. None of the methods in the Math
class, such as random, pow, sin, and cos, is dependent on a specific instance. Therefore, these
methods are static methods. The main method is static and can be invoked directly from a class.
也就是说,看该方法的功能或变量是否依赖于具体的对象,就像一个圆的半径,肯定是依赖于各个对象的。而决定一个方法是否是static就看他实现的功能是否与具体对象相关联,如果不是的话,那么就定义为static