例3.17
JFinalTest.java

public class JFinalTest  
{
    final static int number = 100;

    public void setValue(final int a, int b)
    {
        //a = b;
        b = 10;
    }

    public static void main(String args[])
    {
        // 下列语句去除注释后发生编译错误,final修饰的变量number无法再次赋值
        // JFinalTest.number=200;
        final JPerson2 s1 = new JPerson2(); // 实例化例3.13的JPerson2类
        JPerson2 s2 = new JPerson2();
        // 下列语句去除注释后发生编译错误,final修饰的变量s1无法再次赋值
        // s1=s2;
        s1.setValue(30, "美国");
        System.out.println("s1.age的值为" + s1.age + "  s1.counrty的值为"
                + s1.country);
    }
}

例3.18
JFinalBook.java

public class JFinalBook  
{
    public final int bookID;
    public static int booknumber = 1;

    public JFinalBook()
    {
        bookID = booknumber++;
    }

    public long getID()
    {
        return bookID;
    }

    public static void main(String[] args)
    {
        JFinalBook[] s = new JFinalBook[5];
        for (int i = 0; i < s.length; i++)
        {
            s[i] = new JFinalBook();
            System.out.println("The bookID is " + s[i].getID());
        }
    }
}

注:本博客内容节选自高飞编著的Java程序设计实用教程 ,详细内容请参阅书籍。