static

2018-03-12  本文已影响0人  sonan_2a32

static是什么?

Static是java里的一个非访问修饰符(Non Access Modifier),可以被用在变量,方法,嵌套类和静态block上。

1. 静态变量

As static variables are available for the entire class so conceptually it can only be declared after the class whose scope is global where as static block or methods all have their own scope.

但是这在c/c++中是允许的
From wikipedia:

"Static local variables: variables declared as static inside inside a function are statically allocated while having the same scope as automatic local variables. Hence whatever values the function puts into its static local variables during one call will still be present when the function is called again."

2. 静态方法

3. 静态方法能否被重载

, Read More..

4. 静态方法能否被覆盖

不能, 因为JAVA里不会有 Run-time Polymorphism. Read More..

5. 为什么main()方法要是静态的

因为如果main方法不是静态的,要先实例化对象才能调用main,会需要额外的内存分配。

6. 静态块

7. 静态类

8. 构造函数可以是static的吗?

不能,会报错“modifier static not allowed here”

8. 为什么抽象方法不能是static的?

抽象方法不能被调用,而static方法可直接被调用,矛盾。

9. interface中不能有static方法

因为接口中所有方法是隐式abstract的。

10. abstract类可以有static方法吗

可以。

上一篇 下一篇

猜你喜欢

热点阅读