一、概念
static起到修饰静态的作用,可以修饰变量和方法。
静态变量又叫类变量,从内存的角度来看,如论该变量如何被引用如何被实例化,都只占用一块内存,不再分配新的。
静态方法:有一种说法是这样的,因为Java中方法必须放在类中,而有些方法是全局的,就采取了static修饰。总而言之就是独立于类的方法
他们都可以通过类名.方法/类名.变量 获得
二、实践
public class Test {
static int a=4;
int b=5;
static void test(){
System.out.println("this is a static method");
}
void test1(){
System.out.println("this is a normal method");
}
public static void main(String[] args) {
Test.a=6;
System.out.println(a); //在一个类中可以省略类名:6
test();//:this is a static method
// test1();//报错
Test q=new Test();
System.out.println(q.a); //因为只分配了一块地方,上面改了值后,还是那块值:6
}
}