未完 java Static 总结

[b]static可以用来修饰:[/b]
字段 Fields
方法 Methods
嵌套类 Nested Classes
初始化块 Initialization Block
import (注意格式为[b]import static[/b],不是static import)


[b]Static Fields:[/b]
[url]http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.1.1[/url]
常称其为类变量或静态变量。
类变量在类实例化
Static Field在类装载的时候进行初始化;它是class 。。。的,所以如果有类的多个实例存在,他们Static Field
incarnated


[b]Static Methods:[/b]


[b]Static Nested Class:[/b]
静态嵌套类一般不归入内部类(Inner Class),因为其行为上更像是一个独立的类。详见:
[url]http://wuaner.iteye.com/admin/blogs/573256[/url]


[b]Static Initialization Blocks:[/b]
静态初始化块见: [url]http://wuaner.iteye.com/blog/1669127[/url]


[b]Static Import:[/b]
Sources:[url]http://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html[/url][quote]The static import declaration is analogous to the normal import declaration. Where the [b]normal import declaration imports classes from packages, allowing them to be used without package qualification, the [color=red]static import declaration imports static members from classes, allowing them to be used without class qualification.[/color][/b]
两种格式:
1 引入单个静态成员(这里的静态成员指类变量和静态方法)
import static java.lang.Math.PI;
import static java.lang.String.valueOf;
2 引入类的所有静态成员:
import static java.lang.Math.*;
[/quote]如果当前类中有同名(同名的理解:类变量考虑的就是变量名;静态方法只考虑方法签名中的方法名,不考虑返回值、参数等)的静态成员,优先使用当前类的静态成员。

import static java.lang.String.valueOf;

public class StaticTest {

public static void main(String[] args) {
int i = 1;
/**
* 下面的方法调用会报错,尽管String.valueOf(int)严格匹配该方法调用
* 但根据同名则本类优先原则,使用的是本类中的valueOf(String)方法,出现参数类型不一致的编译错误
*
*/
valueOf(i);
}

static void valueOf(String str) {
}
}



静态代码的执行与反射
类中的静态代码块会在类(XXX.class)被加载到内存的时候执行;注意此时只是被加载,并不一定有该类的对象实例存在。
如通过反射加载:
Class c = Class.forName("XXX");
执行完上句后,XXX.class被加载到内存,并且该类的静态代码块被执行;但是并没有创建该类的对象实例。


Tips about Static:
Static Method中不能使用this。
http://www.velocityreviews.com/forums/t132767-this-compiler-error.html
不能在类的Static Method中访问该类的non-static Method 和 non-static Field。[quote]
[url]http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html[/url]

Instance methods can access instance variables and instance methods directly.
Instance methods can access class variables and class methods directly.
Class methods can access class variables and class methods directly.
Class methods cannot access instance variables or instance methods directly—they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.[/quote]
static不能用来修饰局部变量。


java中没有常量的定义(const为关键字,但未使用)。我们通常结合使用static和final来实现java中的常量:
[url]http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html[/url]


Sources:
Java Tutorials - Understanding Instance and Class Members:
[url]http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值