Static
目前如果想要调用某个对象上面的方法,必须要产生对象的实例,如果在两个地方调用就需要产生两个实例。有些时候被调用的方法与实例的多少没有任何关系,该方法可能只是帮助方法。在这种情况下,不需要多个实例,为了解决必须产生实例才能调用方法的问题,java 引入了static,先看一个实例:
public static void main(String[] args) {
System.out.println(Math.random());
}
在jdk中Math的random方法的作用是提供一个产生随机数的功能,它只是一个帮助方法,与Math中的实例变量没有任何关系,所以不需要调用一次产生一个实例,只通过类直接调用就可以了,在此JDK中就提供了random这static(静态)方法。
Static 不仅可以修饰方法还可以修饰属性,由于Static 修饰的方法或者属性与实例的多少没有任何的关系,可以理解为static 的方法和属性是可以在多个实例之间共享的。
看以下实例:
public class StaticDemo {
static int counter;
public StaticDemo(){
counter++;
}
public static void main(String[] args) {
StaticDemo sd0 = new StaticDemo();
StaticDemo sd1 = new StaticDemo();
System.out.println("counter:"+counter);
}
}
运行结果:counter:2
从以上实例可以看出,sd0 所指向的对象和 sd1指向的对象共享了static变量。实际上我们经常提到的入口函数也是个static静态方法,静态方法与实例没有任何的关系,它可以直接调用静态变量。 也可以通过实例的引用调用静态属性或者方法,效果是相同,只是不需要而已。
以上述实例为例:
System.out.println("counter:"+sd0.counter);, System.out.println("counter:"+sd1.counter)和 System.out.println("counter:"+counter);三者完全等价。
再强调一遍,在静态方法里可以直接调用static(静态)变量,如果不是静态变量,而是实例变量,是不能够在静态方法中直接调用的,必须要先产生实例,好好理解以下这句话:实例变量是与实例相关的,只有产生了实例,实例变量才有意义。而我们想要调用实例变量,这时候必须要先有实例,所以如果想要调用实例变量必须 new 出个实例。
以上实例,如果把关键字static去掉,则counter就变成了实例变量,如果在static 方法中调用必须先有一个实例。
静态代码块
可以把某段代码直接通过static修饰,看以下实例:
public class StaticDemo {
int counter;
static {
System.out.println("in the static segment...");
}
public StaticDemo(){
System.out.println("in the constuctor segment...");
}
public static void main(String[] args) {
StaticDemo sd0 = new StaticDemo();
}
}
运行结果:
in the static segment...
in the constuctor segment...
大家注意到,通过static修饰的代码块在构造函数之前就执行了,实际上静态代码块是在程序加载(load)的时候执行的,而构造函数是在运行的时候执行的。
静态代码块只加载一次,看以下实例:
public class StaticDemo {
static {
System.out.println("in the static segment...");
}
public StaticDemo(){
System.out.println("in the constuctor segment...");
}
public static void main(String[] args) {
StaticDemo sd0 = new StaticDemo();
StaticDemo sd1 = new StaticDemo();
StaticDemo sd2 = new StaticDemo();
}
}
运行结果:
in the static segment...
in the constuctor segment...
in the constuctor segment...
in the constuctor segment...
从运行结果可以看出static 代码只执行了一次,构造函数执行了三次,也就是静态代码块会
在加载的时候执行,而且只执行一次。
匿名代码块。
没有方法名的代码块可以通过static修饰,那么如果我们不用static修饰,不在代码块上加任何的修饰符,打印顺序会是什么呢?
看以下实例:
public class StaticDemo {
int counter;
{
System.out.println("in the normal segment...");
}
static{
System.out.println("in the static segment...");
}
public StaticDemo(){
System.out.println("in the constuctor segment...");
}
public static void main(String[] args) {
StaticDemo sd0 = new StaticDemo();
}
}
我们注意到在static代码块之前有有没有没有任何修饰没有函数名的代码块,我们称之为匿名函数。匿名函数会在加载后但是在任何函数之前得到执行,实际上匿名函数是与实例有关系的,也就是需要产生实例之后执行,但是运行结果确实在构造函数之前得到执行。实际上在构造函数执行之前,实例已经存在,构造函数的作用只是初始化或者赋值的。在学习构造函数的时候,经常会在构造函数中看到this关键子,比如以下代码:
public Dog(String dogName,int age ){
this.dogName = dogName;
this.age = age;
}
以上的this 代表当前对象,也就是说在执行构造函数中代码的时候对象已经存在,此处的构造函数完成的是赋值操作。所以可以理解为new 的动作是分为几步完成的
1. 产生对象,2。初始化。
public class User {
private int age ;
{
System.out.println("in the segment"+age);
}
public User(){
this.age = 1;
System.out.println("in the constuctor method.."+age);
}
static {
System.out.println("in the static segment..");
}
}
入口函数:
public static void main(String[] args) {
User user = new User();
}
运行结果:
in the static segment..
in the segment0
in the constuctor method..1