在C语言中,static的作用是这样的:
1在函数体,一个被声明为静态的变量在这一函数被调用过程中维持其值不变。
2 在模块内(但在函数体外),一个被声明为静态的变量可以被模块内所用函数访问,但不能被模块外其它函数访问。它是一个本地的全局变量。
3在模块内,一个被声明为静态的函数只可被这一模块内的其它函数调用。那就是,这个函数被限制在声明它的模块的本地范围内使用。
在java语言中,static的作用,让我们看看thinking in java的解释:
The meaning of static
With the this keyword in mind,you can more fully understand what it means to make a method static.It means that there is no this for that particular method.You cannot call non - static methods from inside static methods (although the reverse is possible), and you can call a static method for the class itself, without any object.In fact, that’s primarily what a static method is for.It’s as if you’re creating the equivalent of a global function(from C).However, global functions are not permitted in Java, and putting the static method inside a class allows it access to other static methods and to static fields.
Some people argue that static methods are not object - oriented, since they do have the semantics of a global
function; with a static method, you don’t send a message to an object, since there’s no this.This is probably a fair argument, and if you find yourself using a lot of static methods, you should probably rethink your strategy.
看完这段描述之后,我们可以简单这样理解,static可以实现全局变量的功能,static 方法是拿过来就能用的方法,当然static还可以定义静态函数块。