final:在java中,final一般是指不可变的,是一个修饰符,可以修饰常量、方法、类,
public class TestOne{
final int MAX_ONE=1;
public void test(){
MAX_ONE=2;//在这里是错误的,变量被final修饰后不能再赋值
}
}
public class TestOne{
final int MAX_ONE=1;
public final void test(){
String name="sdf";
}
}
class TestTwo extends TestOne{
public final void test(){
String name="sabc";//在这里是错误的,继承后final方法不能被重写
}
}
public final class TestOne{
final int MAX_ONE=1;
public final void test(){
String name="sdf";
}
}
class TestTwo extends TestOne{//在这是错误的,final类不能被继承
}
finally:finally是异常处理的一部分,
Java 中的 Finally 关键一般与try一起使用,在程序进入try块之后,无论程序是因为异常而中止或其它方式返回终止的,finally块的内容一定会被执行 。
以下实例演示了如何使用 finally 通过 e.getMessage() 来捕获异常(非法参数异常)
public class ExceptionDemo2 {
public static void main(String[] argv) {
new ExceptionDemo2().doTheWork();
}
public void doTheWork() {
Object o = null;
for (int i=0; i<5; i++) {
try {
o = makeObj(i);
}
catch (IllegalArgumentException e) {
System.err.println
("Error: ("+ e.getMessage()+").");
return;
}
finally {
System.err.println("都已执行完毕");
if (o==null)
System.exit(0);
}
System.out.println(o);
}
}
public Object makeObj(int type)
throws IllegalArgumentException {
if (type == 1)
throw new IllegalArgumentException
("不是指定的类型: " + type);
return new Object();
}
}
finalize:finalize-方法名。Java 技术允许使用 finalize() 方法在垃圾收集器将对象从内存中清除出去之前做必要的清理工作。
package Initialization;
class Tank{
int howFull = 0;
Tank() { this(0);}
Tank(int fullness){
howFull = fullness;
}
void sayHowFull(){
if(howFull == 0)
System.out.println("Tank is empty!");
else
System.out.println("Tank filling status : " + howFull);
}
void empty(){
howFull = 0;
}
protected void finalize(){
if(howFull != 0){
System.out.println("Error: Tank not empty." + this.howFull);
}
//Normally,you'll also do this:
//Super.finalize(); //call the base-class version
}
}
public class TankTest {
public static void main(String[] args) {
Tank tank1 = new Tank();
Tank tank2 = new Tank(3);
Tank tank3 = new Tank(5);
tank2.empty();
//Drop the reference,forget to cleanup:
new Tank(6);
new Tank(7);
System.out.println("Check tanks:");
System.out.println("tank1:");
tank1.sayHowFull();
System.out.println("tank2:");
tank2.sayHowFull();
System.out.println("tank3");
tank3.sayHowFull();
System.out.println("first forced gc()");
System.gc();
System.out.println("try deprecated runFinalizerOnExit(true)");
System.runFinalizersOnExit(true);
System.out.println("last forced gc():");
System.gc();
}
}