-------------------------------------- Java Sag is under construction: How do I use the final keyword in Java?
Please inform us if you find a non good advertisement : info.enim dot gmail.com

Best Web Hosting

mercredi 9 mai 2012

How do I use the final keyword in Java?

The final modifier is used to mark a class final so that it cannot inherited, to prevent a method being overridden, and to prevent changing the value of a variable. Arguments of a method if declared as final is also can not be modified within the method.
package org.kodejava.example.fundametal;

public class FinalExample {
    //
    // breed is declared final. 
    // can't change the value assigned to breed
    //
    public final String breed = "pig";   
    private int count = 0;

    // 
    // sound() method is declared final, so it can't be overridden
    //
    public final void sound() {     
        System.out.println("oink oink");
    }

    // 
    // number parameter is declared final. can't change the value 
    // assigned to number
    //
    public int count(final int number) {
        //
        // assign a value to number variable will cause a 
        // compile-time error
        //
        number = 1;
        
        count = +number;
        return count;
    }

    public static void main(String[] args) {
        FinalExample fe = new FinalExample();
        // 
        // assign a value to breed variable will cause a 
        // compile-time error
        //
        fe.breed = "dog";
        
        int number = fe.count(20);
    }
}

final class SubFinalExample extends FinalExample {

    //
    // try to override sound() method of superclass will cause a 
    // compile-time error
    //
    public void sound() {
         System.out.println("oink");
    }
}

//
// try to inherit a class that declared final will cause a 
// compile-time error
//
class OtherFinalExample extends SubFinalExample {
}

Aucun commentaire:

Enregistrer un commentaire

Best Web Hosting