final,最后的,最终的;决定性的;不可更改的。在java中final可以用来修饰类,方法和属性。
(1)修饰类:则此类不能再被继承
[img]http://dl.iteye.com/upload/attachment/0084/2527/4a5ee73d-e8fe-3c24-b746-7985f246274a.png[/img]
(2)修饰方法:不能再被子类覆写,但可以被重载
[img]http://dl.iteye.com/upload/attachment/0084/2529/48492676-357c-3333-9549-cd1522a020c4.png[/img]
(3)修饰属性:变量即成常量,不可再被改变
[img]http://dl.iteye.com/upload/attachment/0084/2531/1637d74d-50a4-32e5-9532-2e218a765c67.png[/img]
statice静态的,可以用来修饰方法和属性,在java程序中任何变量或者代码都是在编译时由系统自动分配内存来存储的,而所谓静态就是指在编译后所分配的内存会一直存在,直到程序退出内存才会释放这个空间,也就是只要程序在运行,那么这块内存就会一直存在。
(1)修饰方法:用static修饰的方法可以被类中其他方法直接调用而不用实例化 ,而不用static则不能直接调用
[img]http://dl.iteye.com/upload/attachment/0084/2538/62d61509-244a-3ae4-bd90-7bab46ac309c.png[/img]
而不用static的话
[img]http://dl.iteye.com/upload/attachment/0084/2540/fab9890d-b360-37fc-8668-044739c66509.png[/img]
(2)修饰属性:如果属性被static修饰,那么它可以被称为全局变量,即被所有对象共享,这样可以带来很大的方便
不用static修饰:
[img]http://dl.iteye.com/upload/attachment/0084/2555/bc049786-1457-3059-a9e0-4e5ee1725f91.png[/img]
这样假若所有的对象的city属性都要改变为SH,就需要一个一个操作,三个无所谓,但是300个,300000个呢,这就很不方便,所以可以使用static修饰,这样city属性被共享,其中一个改变就全部改变。
[img]http://dl.iteye.com/upload/attachment/0084/2561/3d1e444e-30c4-3538-b1fc-cc33ce3bc324.png[/img]
至于静态块,即用static修饰的代码块,它在类被编译的时候执行一次,优于其他方法执行。而且无论类被实例化几次,静态块只执行一次,而代码块则是实例化一次便执行一次
(1)修饰类:则此类不能再被继承
package TestCode1;
public final class Test1 {
String name;
}
[img]http://dl.iteye.com/upload/attachment/0084/2527/4a5ee73d-e8fe-3c24-b746-7985f246274a.png[/img]
(2)修饰方法:不能再被子类覆写,但可以被重载
package TestCode1;
public class Test1 {
public final void talk(){
System.out.println("Happy");
}
}
[img]http://dl.iteye.com/upload/attachment/0084/2529/48492676-357c-3333-9549-cd1522a020c4.png[/img]
(3)修饰属性:变量即成常量,不可再被改变
[img]http://dl.iteye.com/upload/attachment/0084/2531/1637d74d-50a4-32e5-9532-2e218a765c67.png[/img]
statice静态的,可以用来修饰方法和属性,在java程序中任何变量或者代码都是在编译时由系统自动分配内存来存储的,而所谓静态就是指在编译后所分配的内存会一直存在,直到程序退出内存才会释放这个空间,也就是只要程序在运行,那么这块内存就会一直存在。
(1)修饰方法:用static修饰的方法可以被类中其他方法直接调用而不用实例化 ,而不用static则不能直接调用
[img]http://dl.iteye.com/upload/attachment/0084/2538/62d61509-244a-3ae4-bd90-7bab46ac309c.png[/img]
而不用static的话
[img]http://dl.iteye.com/upload/attachment/0084/2540/fab9890d-b360-37fc-8668-044739c66509.png[/img]
(2)修饰属性:如果属性被static修饰,那么它可以被称为全局变量,即被所有对象共享,这样可以带来很大的方便
不用static修饰:
package TestCode1;
public class People {
String name;
int age;
String city="BJ";
public People(String name,int age){
this.name=name;
this.age=age;
}
public void info(){
System.out.println("姓名:"+this.name+",年龄:"+this.age+",城市:"+this.city);
}
}
[img]http://dl.iteye.com/upload/attachment/0084/2555/bc049786-1457-3059-a9e0-4e5ee1725f91.png[/img]
这样假若所有的对象的city属性都要改变为SH,就需要一个一个操作,三个无所谓,但是300个,300000个呢,这就很不方便,所以可以使用static修饰,这样city属性被共享,其中一个改变就全部改变。
package TestCode1;
public class People {
String name;
int age;
static String city="BJ";
public People(String name,int age){
this.name=name;
this.age=age;
}
public void info(){
System.out.println("姓名:"+this.name+",年龄:"+this.age+",城市:"+this.city);
}
}
[img]http://dl.iteye.com/upload/attachment/0084/2561/3d1e444e-30c4-3538-b1fc-cc33ce3bc324.png[/img]
至于静态块,即用static修饰的代码块,它在类被编译的时候执行一次,优于其他方法执行。而且无论类被实例化几次,静态块只执行一次,而代码块则是实例化一次便执行一次
package TestCode1;
public class Test1 {
static {//静态块
System.out.println("执行了静态块");
}
{//代码块
System.out.println("执行了代码块");
}
public Test1(){
System.out.println("执行了构造方法");
}
}
package TestCode1;
public class Test2 {
public static void main(String args[]){
new Test1();
new Test1();
new Test1();
new Test1();
}
}
结果:
执行了静态块
执行了代码块
执行了构造方法
执行了代码块
执行了构造方法
执行了代码块
执行了构造方法
执行了代码块
执行了构造方法