java中为什么有static方法

在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还可以定义静态函数块。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值