java中static关键字

static关键字可用于3种场景:

  • 静态变量
  • 静态方法
  • 静态代码块

静态是一个语句块Java class,它将在类首次加载到JVM时执行

  • 静态方法也不能被覆盖,因为它是类的一部分而不是对象?
  • 为了能够从静态方法访问非静态变量,它们需要是静态成员变量。

Details on Static method

  • How to identify? Just check this first. Do you need an class object to access static method? If you don’t need an object then it’s Static method.
  • You need to use static keyword in order to specify static method
  • Better use keyword static if that method is not going to change throughout your project at runtime.
  • You can’t override static method.

我们必须创建Class的实例来访问非静态方法。

当使用关键字声明变量时static,它被称为a class variable。所有实例共享变量的同一副本。可以使用类直接访问类变量,而无需创建实例

Benefits of static variables:

  • constants can be defined without taking additional memory (one for each class)
  • constants can be accessed without an instantiation of the class

Benefits of static methods:

  • instance-independent behavior can be defined without fear of accidental interaction with an instance of the class
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值