static
(摘自百度百科)
static创建的类成员独立于该类的任何对象。不需要通过对象访问。
static函数最常见的例子是main();
声明为static的变量为静态变量或类变量。可以通过类名来引用静态变量,也可以通过实例来引用静态变量。但最好采用前者,以防与普通变量产生混淆。类的所有实例共同拥有一个实例变量。
声明为static的方法为静态方法或类方法。静态方法可以直接调用静态方法,访问静态变量,但不能直接访问实例方法和实例变量。静态方法中不能使用this关键字,因为静态方法不属于任何一个实例。
例子:
// Demonstrate static variables,methods,and blocks.
class UseStatic{
static int a = 3; //static variable
static int b; //static variable
static void meth(int x) //static method
{
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
static //static blocks,初始化静态变量
{
System.out.println("Static block initialized.");
b = a * 4;
}
public static void main(String args[]) {
meth(42);
}
}
执行过程:
1. a=3;
2. b=0;
3. 运行static块,完成通过计算对静态变量赋值:b = 4*3 = 12;
4. 调用mian();
5. main()调用meth函数;
6. 结果:x = 42; a = 3; b = 12.
使用static修饰符的变量或方法属于类本身,而不是类的实例对象。
static class CompanyEmployee{//静态类
public string GetCompanyName(string name) { ... } //没有Static
public static string GetCompanyAddress(string address) { ... }
}
String M_stringA =CompanyEmployee.GetCompanyAddresses(M_string2);//直接引用
CompanyEmployee M_CompE = new CompanyEmployee();//声明一个类的实体
String M_stringN =M_CompE.GetCompanyName(M_string2);//通过实体来引用
final
(参考“浅析Java中的final关键字”)
用final修饰一个类时,表明这个类不能被继承。如果不想该方法的含义在子类中被修改,就使用final修饰。
synchronized
(详见“Java中synchronized的用法”)
java线程同步中的同步锁。
abstract
(参见java中abstract详解)
修饰类和方法时,表示抽象类/抽象方法。
例子:
public abstract String myMethod();
native
先放个链接,用得着的时候再具体看。