Final, Finally (Part 2: Final Class)

welcome to part 2 of Final Keyword. Lets Start …

Final Class

We can declare a class as final. After that we cant create child class under that final class.

example time.


public final class King {
    public static void main (String args []){
        System.out.println("king Arthur");   
    }
}

public class Test extends King {
    
}

// compile error: cannot inherit from final King

so why on earth you want a final class,

1. Final classes (Immutable) are read-only.
2. Thread safe, no synchronization overhead.

a very good explanation

Enjoy 🙂

Advertisements

Final, Finally (Part 1: Final Variables)

Well The Java keyword final has nothing to do with this tv series 🙂

Final Variable

A final variable cannot be changed after it has been assigned a value.

Java convention says that you should write you final constants in uppercase and use underscore (_) to separate multiple words. Here is a basic example

public class Test {
    public static void main (String args []) {
        final int SECOND = 60;
        final String WATCH_NAME = "SNOW";
        System.out.println(SECOND);
        System.out.println(WATCH_NAME);
    }
}

Let’s messing around

        final int SECOND = 60;
        final String WATCH_NAME = "SNOW";
        System.out.println(SECOND);
        System.out.println(WATCH_NAME);
        SECOND = 60; // error

Compile time error: cannot assign a value to final variable SECOND

hm seems nice

public class Test {
        final int SECOND = 60;
        final String WATCH_NAME = "SNOW";
}

public class IwantToChangeYou {
    public static void main (String args []) {
        Test t = new Test();
        t.SECOND = -60; // error
    }
}

Compile time error in IwantToChangeYou class: cannot assign a value to final variable SECOND in class.

public class Test {
    final int SECOND = 60;
    final String WATCH_NAME = "SNOW";
    Test(int val){
        this.SECOND = val; // error
    }
}

public class IwantToChangeYou {
    public static void main (String args []) {
        Test t = new Test(60);
    }
} 

Compile time error in Test class: cannot assign a value to final variable SECOND.


/* A blank final is a final variable whose declaration lacks an initializer. A blank final instance variable of a class must be definitely assigned at the end of every constructor of the class in which it is declared */

public class IwantToChangeYou {
    
    final int SECOND; 
    
    IwantToChangeYou(){
        this.SECOND = 60;
    }
   
    public static void main (String args []) {
        System.out.println("yes we can set the final value at run time!");
    }
    
}
// but if we use static final we cant do this 

public class IwantToChangeYou {
    
    static final int SECOND; // A blank final is a final variable whose declaration lacks an initializer. 
    
    IwantToChangeYou(){
        this.SECOND = 60;
    }
   
    public static void main (String args []) {
        System.out.println("yes we can set the final value at run time!");
    }
    
}

// option a
static final int SECOND  = 10; 

//option b
public class IwantToChangeYou {
    
    static final int SECOND;
    
    static {
        SECOND = 10;
    }
   
    public static void main (String args []) {
        System.out.println("yes we can set the final value at run time!");
    }
    
}

// we can also use local final variables

public class IwantToChangeYou {
    
    public static void main (String args []) {
        System.out.println("whhat");
    }
    
    public int whaaat(final int NO_WAY){
        int lol = NO_WAY;
        return lol;
    }
}


// Compile time error when we tried to change it
 public int whaaat(final int NO_WAY){
        NO_WAY = 99; // error
        int lol = NO_WAY;
        return lol;
}

public class IwantToChangeYou {
    static final int NO_WAY;
    static int OK;
    IwantToChangeYou(int a){
        this.OK = a;   
        System.out.println("id 1");
    }
    static {
        System.out.println("id 2");
        NO_WAY = OK;
    }  
}

public class Test {
    public static void main (String args []) {
        IwantToChangeYou hm = new IwantToChangeYou(77);
        System.out.println("set final static variable "+hm.NO_WAY);
    } 
}

// expected output ??
// well its 0, because static block executed first when a class initialized
/*
id 2
id 1
set final static variable 0
*/

References
link
link

Enjoy!